Skip to content

Instantly share code, notes, and snippets.

@jpierson
Last active August 2, 2017 03:38
Show Gist options
  • Select an option

  • Save jpierson/88a1be40c2a3eab83d2d271f78293ed6 to your computer and use it in GitHub Desktop.

Select an option

Save jpierson/88a1be40c2a3eab83d2d271f78293ed6 to your computer and use it in GitHub Desktop.

Revisions

  1. jpierson revised this gist Aug 2, 2017. 1 changed file with 31 additions and 1 deletion.
    32 changes: 31 additions & 1 deletion ElmScratchPad.md
    Original file line number Diff line number Diff line change
    @@ -48,4 +48,34 @@ join prefix separator suffix list =

    main =
    text (toString (join "<<<" "::" ">>>" (List.map (\i -> toString i) v)))
    ```
    ```

    Outputs the text `<<<1::2::3>>>`

    ### Join items of a list using generic function using specified prefix, separator, and suffix

    ```elm
    import Html exposing (text)

    v = ['a', 'b', 'c']

    joinHelper : (a -> a -> a) -> a -> a -> List a -> a -> a
    joinHelper append separator suffix list s =
    case list of
    [] -> append s suffixstrings
    h::t -> joinHelper append separator suffix t (append s (append separator h))

    join : (a -> a -> a) -> a -> a -> a -> List a -> a
    join append prefix separator suffix list =
    case list of
    [] -> append prefix suffix
    h::t -> joinHelper append separator suffix t (append prefix h)

    myJoinStrings = join (\ a b -> a ++ b) "<<<" "::" ">>>"

    main =
    text (toString (myJoinStrings (List.map (\c -> String.fromChar c) v)))
    ```


    Outputs the text `<<<a::b::c>>>`
  2. jpierson revised this gist Aug 2, 2017. 1 changed file with 25 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions ElmScratchPad.md
    Original file line number Diff line number Diff line change
    @@ -23,4 +23,29 @@ join separator list =

    main =
    text (toString (join "::" (List.map (\i -> toString i) v)))
    ```

    Outputs the text `1::2::3`

    ### Join a list of strings into a single string with specified separator, prefix and suffix

    ```elm
    import Html exposing (text)

    v = [1,2,3]

    joinHelper : String -> String -> List String -> String -> String
    joinHelper separator suffix list s =
    case list of
    [] -> s ++ suffix
    h::t -> joinHelper separator suffix t (s ++ separator ++ h)

    join : String -> String -> String -> List String -> String
    join prefix separator suffix list =
    case list of
    [] -> ""
    h::t -> joinHelper separator suffix t (prefix ++ h)

    main =
    text (toString (join "<<<" "::" ">>>" (List.map (\i -> toString i) v)))
    ```
  3. jpierson revised this gist Aug 2, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion ElmScratchPad.md
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,6 @@ joinHelper : String -> List String -> String -> String
    joinHelper separator list s =
    case list of
    [] -> s
    -- [a] -> s ++ a
    h::t -> joinHelper separator t (s ++ separator ++ h)

    join : String -> List String -> String
  4. jpierson created this gist Aug 2, 2017.
    27 changes: 27 additions & 0 deletions ElmScratchPad.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    # Elm experiments

    ## Lists

    ### Join a list of strings into a single string with specified separator

    ```elm
    import Html exposing (text)

    v = [1,2,3]

    joinHelper : String -> List String -> String -> String
    joinHelper separator list s =
    case list of
    [] -> s
    -- [a] -> s ++ a
    h::t -> joinHelper separator t (s ++ separator ++ h)

    join : String -> List String -> String
    join separator list =
    case list of
    [] -> ""
    h::t -> joinHelper separator t h

    main =
    text (toString (join "::" (List.map (\i -> toString i) v)))
    ```