Skip to content

Instantly share code, notes, and snippets.

@dschinkel
Last active September 16, 2018 22:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dschinkel/97677cab8d509e152d7d2abf484f06ac to your computer and use it in GitHub Desktop.
Save dschinkel/97677cab8d509e152d7d2abf484f06ac to your computer and use it in GitHub Desktop.
Elm - Repl Examples
{--
Below shows some code commands that if entered into elm repl shows the output received
--}
import List
empty = ' '
markerX = 'X'
markerO = 'O'
gameNode = [ [ markerX, empty, empty ], [ markerO, markerO, empty ], [ markerX, empty, empty ]]
-- repl output: [['X',' ',' '],['O','O',' '],['X',' ',' ']] : List (List Char)
{--
flatten all values in the 2-dimensional array. Since the values of the
array are arrays themselves, then all the arrays inside get flattened
--}
rowsFlattened = List.concat gameNode
-- repl output: ['X',' ',' ','O','O',' ','X',' ',' '] : List Char
-- get the open position values
openPositions = List.filter (\pos -> pos == empty) rowsFlattened
-- repl output [' ',' ',' ',' ',' '] : List Char
alllPositionIndexes = (List.indexedMap (\i pos -> i)) rowsFlattened
|> List.filter (\cell i -> cell == empty)
-- repl output: [0,1,2,3,4,5,6,7,8] : List Int
openPositionIndexes =
listFlattened
|> List.indexedMap (\i pos -> ( i, pos ))
|> List.filter (\( _, pos ) -> pos == empty)
|> List.map Tuple.first
-- repl output: [1,2,5,7,8] : List Int
-- get the open position indexes
-- coming soon
{--
Below shows some code commands that if entered into elm repl shows the output received
--}
import Array
import List
boardAsTwoDimensionalList = [ [ 'X', 'X', ' ' ], [ ' ', ' ', ' ' ], [ ' ', ' ', ' ' ] ]
-- repl output: [['X','X',' '],[' ',' ',' '],[' ',' ',' ']] : List (List Char)
boardAsListWithArrays = List.map Array.fromList boardAsTwoDimensionalList
-- repl output: [Array.fromList ['P','P',' '],Array.fromList [' ',' ',' '],Array.fromList [' ',' ',' ']] : List (Array.Array Char)
boardAsTwoDimensionalArray = Array.fromList boardAsListWithArrays
-- repl output: Array.fromList [Array.fromList ['P','P',' '],Array.fromList [' ',' ',' '],Array.fromList [' ',' ',' ']] : Array.Array (Array.Array Char)
row1 = Array.get 0 boardAsTwoDimensionalArray
-- repl output: Just (Array.fromList ['P','P',' ']) : Maybe.Maybe (Array.Array Char)
{--
Below shows some code commands to enter into elm to test a function.
Function or definitions in the repl need \ appended to the end of each line if it's a multiple line expression
--}
import Array
import List
list = [['X',' ',' '],['O','O',' '],['X',' ',' ']]
twoDimensionalListToArray : List (List a) -> Array.Array (Array.Array a) \
twoDimensionalListToArray list = \
let \
node = \
List.map Array.fromList list \
in \
Array.fromList node
twoDimensionalArray = twoDimensionalListToArray list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment