Skip to content

Instantly share code, notes, and snippets.

@paralax
Created May 30, 2015 00:34
Show Gist options
  • Save paralax/ee46acfba8a370930a88 to your computer and use it in GitHub Desktop.
Save paralax/ee46acfba8a370930a88 to your computer and use it in GitHub Desktop.
import Graphics.Element exposing (..)
import Graphics.Input.Field as Field
import List
import String
{-- http://www.reddit.com/r/dailyprogrammer/comments/2qxrtk/20141231_challenge_195_intermediate_math_dice/cnawmtn --}
{--
based on https://hackage.haskell.org/package/split-0.1.1/docs/Data-List-Split.html
add to https://github.com/circuithub/elm-list-extra/blob/2.3.1/src/List/Extra.elm
module List.Extra
( groupInto
, collect
) where
--}
{-|
@docs groupInto
-}
{-| splitEvery n splits a list into length-n pieces. The last piece will be shorter if n does not evenly divide the length of the list.
-}
splitEvery : Int -> List a -> List (List a)
splitEvery n l =
let
loop : Int -> List a -> List (List a) -> List (List a)
loop n rem acc =
case rem of
[] -> acc |> List.reverse
_ -> loop n (List.drop n rem) ((List.take n rem)::acc)
in
loop n l []
{-| collect iterates over each element of the list, applies the given function. Concatenates all the results and returns the combined list.
-}
collect : (a -> List a') -> List a -> List a'
collect f l = List.map (\x -> f x) l |> List.concat
iteri : (int -> a) -> List a -> List -> ()
iteri = ()
mapi : (int -> a -> a') -> List a -> List a'
mapi f l = zip [0..(List.length l) |> List.map (\(i,x) -> f i x)
{-- http://stackoverflow.com/questions/1526046/f-permutations --}
splitList : List a -> List (List a)
splitList l = case l of
[x] -> [x, []]
x::xs -> (x,xs)::(List.map (\(y, l) -> y,(x::l))) (splitList xs)
permutations : List a -> List (List a)
permutations l = case l of
[] -> [[]]
x::xs -> splitList l
|> collect (\(x, xs) -> permutations xs |> List.map (\l -> x::l))
content : Signal.Mailbox Field.Content
content =
Signal.mailbox Field.noContent
main : Signal Element
main =
Signal.map scene content.signal
scene : Field.Content -> Element
scene fieldContent =
flow down
[ Field.field Field.defaultStyle (Signal.message content.address) "List of stuff?" fieldContent
, show (String.split " " fieldContent.string |> List.filter (\x -> (String.length x) > 0) |> splitEvery 3)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment