Reference is a concept to handle immutable data structure flexibly as using "reference" in mutable languages.
Get focused object from Reference value.
ref : Reference Int (List Int)
ref = fromRecord
{ this = 2
, rootWith = \x -> [1] ++ [x] ++ [3]
}
this ref
--> 2Pick out root object from Reference value.
ref : Reference Int (List Int)
ref = fromRecord
{ this = 2
, rootWith = \x -> [1] ++ [x] ++ [3]
}
root ref
--> [1, 2, 3]Pick out root object from Reference value by specifying this value.
rootWith ref a == root <| modify (\_ -> a) refA constructor for Reference.
A constructor for Reference to create top root object.
ref : Reference (Maybe Int) (Maybe Int)
ref = top (Just 3)
this ref
--> Just 3
root ref
--> Just 3Modify an object. It makes root object also changed.
ref : Reference Int (List Int)
ref = fromRecord
{ this = 2
, rootWith = \x -> [1] ++ [x] ++ [3]
}
modifiedRef : Reference Int (List Int)
modifiedRef = modify (\n -> n * 10) ref
this modifiedRef
--> 20
root modifiedRef
--> [1, 20, 3]Change root object type by providing convert function.
ref : Reference Int (List Int)
ref = fromRecord
{ this = 4
, rootWith = \x ->
x :: [5]
}
rootWith : List Int -> List (List Int)
rootWith ls =
[[2, 3]] ++ [ls] ++ [[6, 7]]
newRef : Reference Int (List (List Int))
newRef = map rootWith ref
this ref
--> 4
root ref
--> [4, 5]
this newRef
--> 4
root newRef
--> [[2,3], [4,5], [6,7]]
modifiedRef : Reference Int (List (List Int))
modifiedRef = modify (\_ -> 8) newRef
this modifiedRef
--> 8
root modifiedRef
--> [[2,3], [8,5], [6,7]]
A core data type to realize references of mutable programing languages in Elm. After modifying target value by
modifyfunction,rootvalue is also updated as an example bellow.