on master help sign in
Use F# style function composition

Acting on advice from https://github.com/elm-lang/elm-lang.org/issues/158 and earlier discussions on the mailing list.

commit3a374704b074c03ca33d9b3646043736961901ce authorEvan Czaplicki <info@elm-lang.org> dateFri 11 Jul 2014, 15:50 (12 years ago) parent74b1b11
3 files changed+39−16
changelog.md+1 compiler/Generate/JavaScript.hs+16−6 libraries/Basics.elm+22−10
changelog.md+1
@@ -17,6 +17,7 @@
1717
1818 * Unambiguous syntax for importing ADTs and type aliases (whoo!)
1919 * sqrt and logBase both only work on Floats now
20+ * (.) is replaced by (<<) and (>>) as in F#
2021
2122## 0.12.3
2223
compiler/Generate/JavaScript.hs+16−6
@@ -369,13 +369,17 @@ binop :: Region -> Var.Canonical -> Canonical.Expr -> Canonical.Expr
369369 -> State Int (Expression ())
370370binop region func@(Var.Canonical home op) e1 e2 =
371371 case (home, op) of
372− (Var.Module "Basics", ".") ->
373− do es <- mapM expression (e1 : collect [] e2)
372+ (Var.Module "Basics", ">>") ->
373+ do es <- mapM expression (collectLeftAssoc [e2] e1)
374+ return $ ["$"] ==> List.foldl' (\e f -> f <| e) (ref "$") es
375+
376+ (Var.Module "Basics", "<<") ->
377+ do es <- mapM expression (e1 : collectRightAssoc [] e2)
374378 return $ ["$"] ==> foldr (<|) (ref "$") es
375379
376380 (Var.Module "Basics", "<|") ->
377381 do e2' <- expression e2
378− es <- mapM expression (collect [] e1)
382+ es <- mapM expression (collectRightAssoc [] e1)
379383 return $ foldr (<|) e2' es
380384
381385 (Var.Module "List", "++") ->
@@ -399,12 +403,18 @@ binop region func@(Var.Canonical home op) e1 e2 =
399403 return (ref "A2" `call` [ Var.canonical func, e1', e2' ])
400404
401405 where
402− collect es e =
406+ collectRightAssoc es e =
403407 case e of
404− A _ (Binop (Var.Canonical (Var.Module "Basics") ".") e1 e2) ->
405− collect (es ++ [e1]) e2
408+ A _ (Binop (Var.Canonical (Var.Module "Basics") "<<") e1 e2) ->
409+ collectRightAssoc (es ++ [e1]) e2
406410 _ -> es ++ [e]
407411
412+ collectLeftAssoc es e =
413+ case e of
414+ A _ (Binop (Var.Canonical (Var.Module "Basics") ">>") e1 e2) ->
415+ collectLeftAssoc (e2 : es) e1
416+ _ -> e : es
417+
408418 basicOps = Map.fromList (infixOps ++ specialOps)
409419
410420 infixOps =
libraries/Basics.elm+22−10
@@ -46,7 +46,7 @@ which happen to be radians.
4646@docs fst, snd
4747
4848# Higher-Order Helpers
49−@docs id, always, (<|), (|>), (.), flip, curry, uncurry
49+@docs id, always, (<|), (|>), (<<), (>>), flip, curry, uncurry
5050
5151-}
5252
@@ -301,21 +301,32 @@ isInfinite = Native.Basics.isInfinite
301301
302302-- Function Helpers
303303
304−{-| Function composition. For example, the following code checks if the square
305−root of a number is odd:
304+{-| Function composition, passing results along in the suggested direction. For
305+example, the following code checks if the square root of a number is odd:
306306
307− not . isEven . sqrt
307+ not << isEven << sqrt
308308
309309You can think of this operator as equivalent to the following:
310310
311− (f . g) == (\x -> f (g x))
311+ (g << f) == (\x -> g (f x))
312312
313−So our example is expanding things out to this:
313+So our example expands out to something like this:
314314
315− \number -> not (isEven (sqrt number))
315+ \n -> not (isEven (sqrt n))
316316-}
317−(.) : (b -> c) -> (a -> b) -> (a -> c)
318−(.) f g x = f (g x)
317+(<<) : (b -> c) -> (a -> b) -> (a -> c)
318+(<<) g f x = g (f x)
319+
320+{-| Function composition, passing results along in the suggested direction. For
321+example, the following code checks if the square root of a number is odd:
322+
323+ sqrt >> isEven >> not
324+
325+This direction of function composition seems less pleasant than `(<<)` which
326+reads nicely in expressions like: `filter (not << isRegistered) students`
327+-}
328+(>>) : (a -> b) -> (b -> c) -> (a -> c)
329+(>>) f g x = g (f x)
319330
320331{-| Forward function application `x |> f == f x`. This function is useful
321332for avoiding parenthesis and writing code in a more natural way.
@@ -344,7 +355,8 @@ This can also be written as:
344355(<|) : (a -> b) -> a -> b
345356f <| x = f x
346357
347−infixr 9 .
358+infixr 9 <<
359+infixl 9 >>
348360infixr 0 <|
349361infixl 0 |>
350362