Scala equivalent of Haskell first and second -


haskell has convenient functions called first , second apply function 1 element of pair:

first fn (a,b) = (fn a, b) second fn (a,b) = (a, fn b) 

are such functions defined in standard scala libraries?

edit: know it's easy define them, possible it's cleaner use standard functions standard names…

def first[a, b, x](fn: => x)(pair: (a, b)): (x, b) = (fn(pair._1), pair._2) def second[a, b, x](fn: b => x)(pair: (a, b)): (a, x) = (pair._1, fn(pair._2)) 

haskell's arrows (first , second among them) implemented in scalaz:

scalaz source

some examples

while it's technically not standard library it's stable , seems maintained.

update

syntax bit cumbersome though (maybe there way?):

import scalaz._ import scalaz._  val f = (x: int) => x + 1 val g = f.second[string] g("1", 2) //> ("1", 3)  // or type inference  f second ("1", 2) //> ("1", 3) 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -