on master help sign in
Merge branch 'fun-comp'
0d3cba6Evan Czaplicki
changelog.md+1
@@ -19,6 +19,7 @@
19#### Breaking Changes:
19#### Breaking Changes:
20
20
21 * Rename Basics.id to Basics.identity
21 * Rename Basics.id to Basics.identity
22+ * (.) is replaced by (<<) and (>>) as in F#
22 * Rename Basics.div to (//)
23 * Rename Basics.div to (//)
23 * Rename Basics.mod to (%)
24 * Rename Basics.mod to (%)
24 * Rename Maybe.maybe to Maybe.extract
25 * Rename Maybe.maybe to Maybe.extract
compiler/Generate/JavaScript.hs+16−6
@@ -369,13 +369,17 @@ binop :: Region -> Var.Canonical -> Canonical.Expr -> Canonical.Expr
369 -> State Int (Expression ())
369 -> State Int (Expression ())
370binop region func@(Var.Canonical home op) e1 e2 =
370binop region func@(Var.Canonical home op) e1 e2 =
371 case (home, op) of
371 case (home, op) of
372− (Var.Module "Basics", ".") ->
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", "<<") ->
373− do es <- mapM expression (e1 : collect [] e2)
377+ do es <- mapM expression (e1 : collectRightAssoc [] e2)
374 return $ ["$"] ==> foldr (<|) (ref "$") es
378 return $ ["$"] ==> foldr (<|) (ref "$") es
375
379
376 (Var.Module "Basics", "<|") ->
380 (Var.Module "Basics", "<|") ->
377 do e2' <- expression e2
381 do e2' <- expression e2
378− es <- mapM expression (collect [] e1)
382+ es <- mapM expression (collectRightAssoc [] e1)
379 return $ foldr (<|) e2' es
383 return $ foldr (<|) e2' es
380
384
381 (Var.Module "List", "++") ->
385 (Var.Module "List", "++") ->
@@ -399,12 +403,18 @@ binop region func@(Var.Canonical home op) e1 e2 =
399 return (ref "A2" `call` [ Var.canonical func, e1', e2' ])
403 return (ref "A2" `call` [ Var.canonical func, e1', e2' ])
400
404
401 where
405 where
402− collect es e =
406+ collectRightAssoc es e =
403 case e of
407 case e of
404− A _ (Binop (Var.Canonical (Var.Module "Basics") ".") e1 e2) ->
408+ A _ (Binop (Var.Canonical (Var.Module "Basics") "<<") e1 e2) ->
405− collect (es ++ [e1]) e2
409+ collectRightAssoc (es ++ [e1]) e2
406 _ -> es ++ [e]
410 _ -> es ++ [e]
407
411
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+
408 basicOps = Map.fromList (infixOps ++ specialOps)
418 basicOps = Map.fromList (infixOps ++ specialOps)
409
419
410 infixOps =
420 infixOps =
libraries/Basics.elm+22−10
@@ -46,7 +46,7 @@ which happen to be radians.
46@docs fst, snd
46@docs fst, snd
47
47
48# Higher-Order Helpers
48# Higher-Order Helpers
49−@docs identity, always, (<|), (|>), (.), flip, curry, uncurry
49+@docs identity, always, (<|), (|>), (<<), (>>), flip, curry, uncurry
50
50
51-}
51-}
52
52
@@ -316,21 +316,32 @@ isInfinite = Native.Basics.isInfinite
316
316
317-- Function Helpers
317-- Function Helpers
318
318
319+{-| Function composition, passing results along in the suggested direction. For
319−{-| Function composition. For example, the following code checks if the square
320+example, the following code checks if the square root of a number is odd:
320−root of a number is odd:
321
321
322− not . isEven . sqrt
322+ not << isEven << sqrt
323
323
324You can think of this operator as equivalent to the following:
324You can think of this operator as equivalent to the following:
325
325
326− (f . g) == (\x -> f (g x))
326+ (g << f) == (\x -> g (f x))
327
327
328−So our example is expanding things out to this:
328+So our example expands out to something like this:
329
329
330− \number -> not (isEven (sqrt number))
330+ \n -> not (isEven (sqrt n))
331-}
331-}
332−(.) : (b -> c) -> (a -> b) -> (a -> c)
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)
333−(.) f g x = f (g x)
344+(>>) f g x = g (f x)
334
345
335{-| Forward function application `x |> f == f x`. This function is useful
346{-| Forward function application `x |> f == f x`. This function is useful
336for avoiding parenthesis and writing code in a more natural way.
347for avoiding parenthesis and writing code in a more natural way.
@@ -359,7 +370,8 @@ This can also be written as:
359(<|) : (a -> b) -> a -> b
370(<|) : (a -> b) -> a -> b
360f <| x = f x
371f <| x = f x
361
372
362−infixr 9 .
373+infixr 9 <<
374+infixl 9 >>
363infixr 0 <|
375infixr 0 <|
364infixl 0 |>
376infixl 0 |>
365
377
libraries/Maybe.elm+1−1
@@ -58,4 +58,4 @@ map : (a -> b) -> Maybe a -> Maybe b
58map f maybe =
58map f maybe =
59 case maybe of
59 case maybe of
60 Just v -> Just (f v)
60 Just v -> Just (f v)
61− Nothing -> Nothing
61+ Nothing -> Nothing