@@ -72,6 +72,31 @@ func ExampleOption_Value() {
7272 // true
7373}
7474
75+ func ExampleMap () {
76+ posOnly := func (x int ) option.Option [int ] {
77+ if x < 0 {
78+ return option .None [int ]()
79+ }
80+
81+ return option .Some (x )
82+ }
83+
84+ doubleNum := func (x int ) int {
85+ return x * 2
86+ }
87+
88+ optDouble := option .Map (doubleNum )
89+
90+ n := posOnly (- 5 )
91+ p := posOnly (25 )
92+
93+ resultN := optDouble (n )
94+ resultP := optDouble (p )
95+
96+ fmt .Println (resultN ) // option.None
97+ fmt .Println (resultP ) // option.Some(50)
98+ }
99+
75100func TestSomeStringer (t * testing.T ) {
76101 assert .Equal (t , fmt .Sprintf ("%s" , option .Some ("foo" )), "Some(foo)" )
77102 assert .Equal (t , fmt .Sprintf ("%s" , option .Some (42 )), "Some(42)" )
@@ -139,3 +164,30 @@ func TestNoneValue(t *testing.T) {
139164 assert .Equal (t , value , 0 )
140165 assert .False (t , ok )
141166}
167+
168+ func TestMapSome (t * testing.T ) {
169+ fn := func (x int ) bool {
170+ return x > 50
171+ }
172+
173+ optFn := option .Map (fn )
174+
175+ resultWithSome := optFn (option .Some (40 ))
176+
177+ unwrappedSome , ok := resultWithSome .Value ()
178+ assert .True (t , ok )
179+ assert .False (t , unwrappedSome )
180+ }
181+
182+ func TestMapNone (t * testing.T ) {
183+ fn := func (x int ) bool {
184+ t .Error ("fn was called!" )
185+ return x > 50
186+ }
187+
188+ optFn := option .Map (fn )
189+ resultWithNone := optFn (option .None [int ]())
190+
191+ _ , ok := resultWithNone .Value ()
192+ assert .False (t , ok )
193+ }
0 commit comments