Last active
September 16, 2018 22:51
-
-
Save dschinkel/97677cab8d509e152d7d2abf484f06ac to your computer and use it in GitHub Desktop.
Elm - Repl Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-- | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-- | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-- | |
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