Skip to content

Commit

Permalink
Implement Elm List functions reverse, member and any
Browse files Browse the repository at this point in the history
And add tests for these functions.
  • Loading branch information
Viir committed Nov 6, 2020
1 parent e3a483d commit 166690a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ elmCoreModulesTexts =
module List exposing (..)
import Char exposing (Char)
cons : a -> List a -> List a
cons element list =
PineKernel.listCons element list
foldl : (a -> b -> b) -> b -> List a -> b
Expand All @@ -316,6 +318,30 @@ length xs =
foldl (\\_ i -> i + 1) 0 xs
reverse : List a -> List a
reverse list =
foldl cons [] list
member : a -> List a -> Bool
member x xs =
any (\\a -> a == x) xs
any : (a -> Bool) -> List a -> Bool
any isOkay list =
case list of
[] ->
False
next :: xs ->
if isOkay next then
True
else
any isOkay xs
drop : Int -> List a -> List a
drop n list =
if n <= 0 then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ evaluatePineApplication context application =
"PineKernel.listTail" ->
functionExpectingOneArgumentOfTypeList (List.tail >> Maybe.withDefault [] >> PineList)

"PineKernel.listCons" ->
evaluatePineApplicationExpectingExactlyTwoArguments
{ mapArg0 = evaluatePineExpression context
, mapArg1 = evaluatePineExpression context
, apply =
\leftValue rightValue ->
case rightValue of
PineList rightList ->
Ok (PineList (leftValue :: rightList))

_ ->
Err "Right operand for listCons is not a list."
}
application.arguments

"String.fromInt" ->
case application.arguments of
[ argument ] ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["c","b","a"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
List.reverse [ "a", "b", "c" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[False,True,False]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ List.member "a" [ "ab", "ba" ], List.member "hello" [ "test", "hello" ], List.member True [ False ] ]

0 comments on commit 166690a

Please sign in to comment.