Skip to content

Instantly share code, notes, and snippets.

@rupeshtiwari
Last active October 25, 2017 01:41
Show Gist options
  • Save rupeshtiwari/a95d256c81fc0e9e48c0e4d73e706e8a to your computer and use it in GitHub Desktop.
Save rupeshtiwari/a95d256c81fc0e9e48c0e4d73e706e8a to your computer and use it in GitHub Desktop.
learning ELM Type System
import Html exposing (text)
{-
type List a
= Nil -- []
| Cons a (List a) -- ::
-}
l1 = []
l2 = 1 :: []
l3 = 1 :: (2 :: [])
l4 = [1,2]
main =
l3 == l4
|> toString
|> text
import Html exposing (text)
type alias Person =
{
name:String, age: Int, occupation : String
}
jem : Person
jem = {name="Jem", age=44, occupation= "Analyst"}
bread = {name="Bread", category ="carbs"}
promote: Person -> Person
promote p =
{p | occupation = "Manager"}
main =
--text (toString(promote jem)) --{ name = "Jem", age = 44, occupation = "Manager" }
jem |> promote |> toString |> text --{ name = "Jem", age = 44, occupation = "Manager" }
----------
import Html exposing (text)
type alias Person =
{
name:String, age: Int, occupation : String, dog: Dog, salary : Float
}
type alias Dog =
{
name:String, breed: String, sterilized : Bool
}
joe : Person
joe = {name="Joe", age=44, occupation= "Analyst" , dog = fido, salary=10000}
fido: Dog
fido = {name="Fido", breed ="Husky", sterilized = False}
promote: Person -> Person
promote p =
{p | occupation = "Manager"
, salary = p.salary*1.2}
sterilizePet : Person -> Person
sterilizePet p =
let
dog = p.dog
in
{p | dog = {dog | sterilized = True}}
main =
joe |> promote |> sterilizePet |> toString |> text
-- below greet function can take both bread or person
import Html exposing (text)
ram = {name="Ram", age=44}
bread = {name="Bread", category ="carbs"}
greet r = "Hello " ++ r.name
main =
text (greet bread)
-----------------
import Html exposing (text)
type alias Person =
{
name:String, age: Int
}
ram : Person
ram = {name="Ram", age=44}
bread = {name="Bread", category ="carbs"}
greet : Person -> String
greet r = "Hello " ++ r.name
main =
text (greet bread)
-- Records in elm is just like javascript objects.
--http://elmrepl.cuberoot.in/ REPL
--http://elm-lang.org/examples/hello-html example
ram = { name = "Ram", age = 44}
ram.name --output "Ram":String
--field accessor to fetch property value.
.name ram --output "Ram"
.age ram --output 40
sita = {name="Sita",age=30}
l1 = [ram, sita]
--[{ name = "Ram", age = 44 },{ name = "Sita", age = 43 }]
import Html exposing (text, h1)
type EmailAddress = EmailAddress String --union type
type alias Message =
{
recipient : EmailAddress
, body : String
}
message : Message
message =
{
recipient= EmailAddress "hello"
, body="hello"
}
validateAddress : String -> Result String EmailAddress
validateAddress s =
if String.contains "@" s
then Result.Ok (EmailAddress s)
else Result.Err "Not a valid email address"
data : String
data = "hello@so.com"
emailToString : EmailAddress ->String
emailToString (EmailAddress s) = s
main =
let content =
data
|> validateAddress
|> Result.map emailToString
|> Result.withDefault "invalid"
in h1 [] [text content]
import Html exposing (div,text,Html)
import Html.Attributes exposing (style)
type alias Rectangle = (Int, Int)
rect : Rectangle
rect = (200,50)
render : Rectangle -> Html a
render (w,h) =
div
[
style
[("backgroundColor", "red"),
("width", (toString w)++"px"),
("height", (toString h)++"px")]
]
[text "Hello,world"]
main = render rect
import Html exposing (text)
data : Rating
data = Excellent
-- Excellent
-- data = Bad -- compile error
main = text (toString data)
type Rating = Excellent
| Good
| Poor
--------------------pattern matching
import Html exposing (text)
data : Rating
data = Other "Appalling"
-- Excellent
-- data = Bad -- compile error
main = text (render data)
type Rating = Excellent
| Good
| Poor
| Other String
render : Rating -> String
render rating =
case rating of
Other s -> s
_ -> toString rating
------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment