You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What's the answer to Exercise 10 in the Many Faces of Map?
Derive a relationship between r, any function f: (A) -> B, and the map on arrays and optionals. This relationship is the “free theorem” for r’s signature.
I have come up with the following examples of an r and an s:
func r<A>(_ xs:[A])->A?{
xs.first
}r([]) //nil
r([1,2]) //1
func s<A,B>(_ f:@escaping(A)->B, _ xs:[A])->B?{letvalue:B?switch xs.last {case.some(let a):
value =f(a)case.none:
value =nil}return value
}s({$0*2},[]) //nil
s({$0*2},[1,2]) //4
I'm not even entirely sure I understand the question. What does it mean to have a relationship between three functions (r, f, map)?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
What's the answer to Exercise 10 in the Many Faces of Map?
I have come up with the following examples of an
r
and ans
:I'm not even entirely sure I understand the question. What does it mean to have a relationship between three functions (r, f, map)?
Any hints? 😃
Beta Was this translation helpful? Give feedback.
All reactions