on master help sign in
Merge branch 'fun-comp'

Conflicts: libraries/Basics.elm libraries/Maybe.elm

commit0d3cba63e0210bc6aeeff59d9383ae2d8b93e222 authorEvan Czaplicki <info@elm-lang.org> dateFri 22 Aug 2014, 23:35 (12 years ago) parentdb5a2923a37470
4 files changed+40−17
changelog.md+1 compiler/Generate/JavaScript.hs+16−6 libraries/Basics.elm+22−10 libraries/Maybe.elm+1−1
changelog.md+1
@@ -19,6 +19,7 @@
1919#### Breaking Changes:
2020
2121 * Rename Basics.id to Basics.identity
22+ * (.) is replaced by (<<) and (>>) as in F#
2223 * Rename Basics.div to (//)
2324 * Rename Basics.mod to (%)
2425 * Rename Maybe.maybe to Maybe.extract
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 identity, always, (<|), (|>), (.), flip, curry, uncurry
49+@docs identity, always, (<|), (|>), (<<), (>>), flip, curry, uncurry
5050
5151-}
5252
@@ -316,21 +316,32 @@ isInfinite = Native.Basics.isInfinite
316316
317317-- Function Helpers
318318
319−{-| Function composition. For example, the following code checks if the square
320−root of a number is odd:
319+{-| Function composition, passing results along in the suggested direction. For
320+example, the following code checks if the square root of a number is odd:
321321
322− not . isEven . sqrt
322+ not << isEven << sqrt
323323
324324You can think of this operator as equivalent to the following:
325325
326− (f . g) == (\x -> f (g x))
326+ (g << f) == (\x -> g (f x))
327327
328−So our example is expanding things out to this:
328+So our example expands out to something like this:
329329
330− \number -> not (isEven (sqrt number))
330+ \n -> not (isEven (sqrt n))
331331-}
332−(.) : (b -> c) -> (a -> b) -> (a -> c)
333−(.) f g x = f (g x)
332+(<<) : (b -> c) -> (a -> b) -> (a -> c)
333+(<<) g f x = g (f x)
334+
335+{-| Function composition, passing results along in the suggested direction. For
336+example, the following code checks if the square root of a number is odd:
337+
338+ sqrt >> isEven >> not
339+
340+This direction of function composition seems less pleasant than `(<<)` which
341+reads nicely in expressions like: `filter (not << isRegistered) students`
342+-}
343+(>>) : (a -> b) -> (b -> c) -> (a -> c)
344+(>>) f g x = g (f x)
334345
335346{-| Forward function application `x |> f == f x`. This function is useful
336347for avoiding parenthesis and writing code in a more natural way.
@@ -359,7 +370,8 @@ This can also be written as:
359370(<|) : (a -> b) -> a -> b
360371f <| x = f x
361372
362−infixr 9 .
373+infixr 9 <<
374+infixl 9 >>
363375infixr 0 <|
364376infixl 0 |>
365377
libraries/Maybe.elm+1−1
@@ -58,4 +58,4 @@ map : (a -> b) -> Maybe a -> Maybe b
5858map f maybe =
5959 case maybe of
6060 Just v -> Just (f v)
61− Nothing -> Nothing
61+ Nothing -> Nothing