0

I try to master Argo for json decoding. In documentations there is example of decode function

extension User: Decodable {
  static func decode(j: JSON) -> Decoded<User> {
    return curry(User.init)
     <^> j <| "id"
     <*> j <| "name"
    }  
}

What does <^> and <*> mean ? What is the difference between them ?

Alexey K
  • 6,537
  • 18
  • 60
  • 118
  • I'd recommend https://github.com/Anviking/Decodable. I used Argo a bit but I think Decodable just does everything much cleaner ever since Swift added support for exceptions. Not as many weird operators. – Andreas Jan 11 '17 at 11:45

2 Answers2

5

According to the Github documentation...

<^> is the map function

and

<*> is the apply function

Both of these are described here... https://robots.thoughtbot.com/functional-swift-for-dealing-with-optional-values

Linked from the Github page.

N.B. Read the documentation :)

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
2

tldr, use <^> for the first parameter and use <*> for every parameter after that.

Long answer: <^> is the same as Swift's map function. It's an operator borrowed from Haskell (a purely functional programming language). Although, in Haskell it's actually <$> but Swift cannot use $ in its operators so we (and others in the community) chose <^> instead.

<^>, aka map, applies a function to a value inside a context. That context can be Optional, Array, Dictionary, or many others. In the context of Optional, <^> will apply the function in the first argument (left of the operator) to the optional value in the second argument (right side of the operator). The result will be .none if the optional value was .none or .some(x) where x is the result of the unwrapped optional value after it is passed into the function.

I hope that makes sense. If so, <*> isn't much different except that the function in the first argument is also wrapped in a context like Optional. So you have an Optional function and an Optional value, then you would use <*>. That is why we use <*> after we use <^> for the first time, because after the first <^>, map, we're left with a partially applied constructor (a function), that has been wrapped in a context by <^>.

<*> is taken from Haskell as well. In Haskell it is called Applicative and uses that exact same operator.