Assigning Unique Callback Functions in Elm - function

I am trying to make a simple Elm webapp that lets me add rectangles to an SVG canvas and drag them around. However, I am running into problems trying to programmatically differentiate the rectangle click handlers. The below code works fine for a single rectangle (mousedown on the shape and move around and it will drag correctly). However, every further rectangle generated somehow has its mousedown function also specifying the first rectangle.
This creates the rectangle with rectID and (I thought) would also create a unique partial function of customOnMouseDown with the rectID parameter of this rectangle.
NewRect rectId ->
let
newRect =
Rect (customOnMouseDown (String.fromInt rectId)) (String.fromInt rectId)
(rectId) 0 20 20
in
( { model |
rects = newRect :: model.rects
, count = model.count + 1}
, Cmd.none)
After trying several different formulations, I think my mental model of Elm's runtime is wrong, so I would like to not only know the correct way to do this sort of thing but also why this way isn't working, if possible.
Full code:
import Browser
import Browser.Events
import Html exposing (..)
import Html.Events
import Task
import Time
import Svg exposing (..)
import Svg.Attributes exposing (..)
import Svg.Events exposing (..)
import Random
import Json.Decode as D
-- MAIN
-- main =
main =
Browser.element
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ drag : Maybe Drag
, pos : Position
, rects : List Rect
, selected : String
, count : Int
}
type alias Position =
{ x: Int
, y: Int
}
type alias Drag =
{ startPos : Position
, currentPos : Position
}
type alias Rect =
{ mouseDown : Html.Attribute Msg
, rectId : String
, x : Int
, y : Int
, width : Int
, height : Int
}
init : () -> (Model, Cmd Msg)
init _ =
( Model Nothing (Position 0 0) [] "" 0
, Cmd.none
)
-- UPDATE
type Msg
= Press Position String
| Release Position
| Move Position
| AddRect
| NewRect Int
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Press pos rectId ->
({model | drag = Just (Drag pos pos)
, selected = rectId
}
, Cmd.none)
Release pos ->
({ model | drag = Nothing, selected = ""}, Cmd.none)
Move pos ->
( { model |
rects =
case (getRect model.selected model.rects) of
Nothing -> model.rects
Just r ->
(Rect r.mouseDown r.rectId pos.x pos.y 20 20)::(dropRect r.rectId model.rects)
}
, Cmd.none )
AddRect ->
( model
, Random.generate NewRect (Random.int 1 1000)
)
NewRect rectId ->
let
newRect =
Rect (customOnMouseDown (String.fromInt rectId)) (String.fromInt rectId)
(rectId) 0 20 20
in
( { model |
rects = newRect :: model.rects
, count = model.count + 1}
, Cmd.none)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
case model.drag of
Nothing ->
Sub.none
Just _ ->
Sub.batch [ Browser.Events.onMouseMove mouseMoveDecoder
, Browser.Events.onMouseUp mouseReleaseDecoder ]
mouseMoveDecoder : D.Decoder Msg
mouseMoveDecoder =
D.map Move mouseCoordDecoder
mouseReleaseDecoder : D.Decoder Msg
mouseReleaseDecoder =
D.map Release mouseCoordDecoder
mouseCoordDecoder : D.Decoder Position
mouseCoordDecoder =
D.map2 Position
(D.field "x" D.int)
(D.field "y" D.int)
-- VIEW
view : Model -> Html Msg
view model =
let
total_width = "1000"
total_height = "500"
in
div []
[ svg
[ width total_width
, height total_height
, viewBox ("0 0 " ++ total_width ++ total_height)
]
(renderShape model.rects)
, div [] [ div [] [ Html.text (String.fromInt model.pos.x) ]
, div [] [ Html.text (String.fromInt model.pos.y) ]
, div [] [ Html.text model.selected ]
, div [] [ Html.text (String.fromInt (List.length model.rects)) ]
, div [] [ (renderList (List.map .rectId model.rects)) ]
, button [ onClick AddRect ] [ Html.text "Rect" ] ]
]
renderList : List String -> Html msg
renderList lst =
ul []
(List.map (\l -> li [] [ Html.text l ]) lst)
customOnMouseDown : String -> (Html.Attribute Msg)
customOnMouseDown shapeIndex =
let
decoder =
D.oneOf
[ D.map2
Press
( D.map2
Position
( D.field "pageX" D.int)
( D.field "pageY" D.int)
)
(D.succeed ( shapeIndex ))
, D.succeed (Press ( Position 500 500 ) shapeIndex )
]
in
Html.Events.on "mousedown" decoder
extractRect : Rect -> Svg Msg
extractRect r =
rect [ r.mouseDown
, x (String.fromInt r.x)
, y (String.fromInt r.y)
, width (String.fromInt r.width)
, height (String.fromInt r.height)
]
[]
renderShape : List Rect -> List (Svg Msg)
renderShape lst =
List.map extractRect lst
rectIdMatch : String -> Rect -> Bool
rectIdMatch target rect = target == rect.rectId
getRect : String -> List Rect -> (Maybe Rect)
getRect target lst =
List.head (List.filter (rectIdMatch target) lst)
dropRect : String -> List Rect -> List Rect
dropRect target lst =
case lst of
[] -> []
[x] ->
if x.rectId == target then
[]
else
[]
x::xs ->
if x.rectId == target then
xs
else
x::(dropRect target xs)

Per glennsl https://ellie-app.com/76K6JmDJg4Fa1]1
Changing the JSON decoder seemed to fix the issue, although I'm not sure why
customOnMouseDown : String -> (Html.Attribute Msg)
customOnMouseDown shapeIndex =
let
decoder =
D.oneOf
[ D.map2
Press
( D.map2
Position
( D.field "pageX" D.int)
( D.field "pageY" D.int)
)
(D.succeed ( shapeIndex ))
, D.succeed (Press ( Position 500 500 ) shapeIndex )
]
in
Html.Events.on "mousedown" decode

Related

How to serialize F# records to JSON properly?

I have a few types in F# like:
type ResourceRecordSet =
| Alias of Name : string *
Type : ResourceRecordType *
AliasTarget : AliasTarget
| Record of Name : string *
Type : ResourceRecordType *
ResourceRecords : List<string> * TTL : uint32
Using the type:
let r =
Record(
"domain.tld."
, SOA
, ["ns-583.awsdns-08.net.
awsdns-hostmaster.amazon.com.
1 7200 900 1209600 86400"]
, 900u
)
When I try to serialize it to JSON I get the following:
let rjson = JsonSerializer.Serialize(r)
sprintf "%A" rjson
Output:
"{"Case":"Record","Fields":["doman.tld.",{"Case":"SOA"},["ns-583.awsdns-08.net. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"],900]}"
Is there a way to control the serialization and produce the following instead:
{
"Name": "doman.tld.",
"ResourceRecords": [ {"Value": "ns-583.awsdns-08.net. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400" }],
"TTL": 900,
"Type": "SOA"
}
To answer my own question, after reading up on the different libraries suggested by different people, Fleece seems like the most solid solution here.
First a simple example:
open System.Text.Json
open Fleece.SystemTextJson
open Fleece.SystemTextJson.Operators
open FSharpPlus
type AliasTarget =
{
DNSName : string
EvaluateTargetHealth : bool
HostedZoneId : string
}
static member ToJson (a: AliasTarget) =
jobj [
"DNSName" .= a.DNSName
"EvaluateTargetHealth" .= a.EvaluateTargetHealth
"HostedZoneId" .= a.HostedZoneId
]
static member OfJson json =
match json with
| JObject o ->
monad {
let! dnsName = o .# "DNSName"
let! evaluateTargetHealth = o .# "EvaluateTargetHealth"
let! hostedZoneId = o .# "HostedZoneId"
return {
DNSName = dnsName
EvaluateTargetHealth = evaluateTargetHealth
HostedZoneId = hostedZoneId
}
}
| x -> Decode.Fail.objExpected x
let outp = aliasTargetToJSON { DNSName = "dbrgct5gwrbsd.cloudfront.net."; EvaluateTargetHealth = false; HostedZoneId = "xxx"}
loggerBlog.LogInfo outp
let aliasJson = """{"DNSName":"dbrgct5gwrbsd.cloudfront.net.","EvaluateTargetHealth":false,"HostedZoneId":"xxx"}"""
let alias : AliasTarget ParseResult = parseJson aliasJson
loggerBlog.LogInfo (sprintf "%A" alias)
This prints:
2020-06-08T23:26:09 INFO [Website] {"DNSName":"dbrgct5gwrbsd.cloudfront.net.","EvaluateTargetHealth":false,"HostedZoneId":"xxx"}
2020-06-08T23:26:09 INFO [Website] Ok { DNSName = "dbrgct5gwrbsd.cloudfront.net."
EvaluateTargetHealth = false
HostedZoneId = "xxx" }
Both the serialization and deserialization works.
ADTs or discriminated unions can be implemented as well:
type Shape =
| Rectangle of width : float * length : float
| Circle of radius : float
| Prism of width : float * float * height : float
with
static member JsonObjCodec =
Rectangle <!> jreq "rectangle" (function Rectangle (x, y) -> Some (x, y) | _ -> None)
<|> ( Circle <!> jreq "radius" (function Circle x -> Some x | _ -> None) )
<|> ( Prism <!> jreq "prism" (function Prism (x, y, z) -> Some (x, y, z) | _ -> None) )
More here:
https://github.com/fsprojects/Fleece

Recursive call in if expression - ocaml

module Dfs = struct
let rec dfslsts g paths final =
let l = PrimePath.removeDuplicates (PrimePath.extendPaths g paths)
in
let f elem =
if (List.mem "%d" (List.flatten final) = false) then (dfslsts g ["%d"] (List.flatten l)::final)
else final
in
List.iter f (Graph.nodes g)
end
Error: This expression has type string but an expression was expected of type int list
This error occurred when I called dfslsts function, which is recursive, inside the if condition.
The function dfslsts returns a list of lists.
If I try to replace the complex expression in if statement to
if (List.mem "%d" (List.flatten final) = false) then "%d"
else "%d"
then I get
Error: This expression has type 'a -> string
but an expression was expected of type 'a -> unit
Type string is not compatible with type unit
at List.iter line.
How do I solve this problem and are we allowed to call a recursive function inside the if expression.
This is the definition of my graph type:
module Graph = struct
exception NodeNotFound of int
type graph = {
nodes : int list;
edges : (int * int) list;
}
let makeGraph () =
{
nodes = [];
edges = [];
}
let rec isNodeOf g n = List.mem n g.nodes
let nodes g = g.nodes
let edges g = g.edges
let addNode g n =
let nodes = n::g.nodes and edges = g.edges in
{
nodes;
edges;
}
let addEdge g (n1, n2) =
if ((isNodeOf g n1) = false) then
raise (NodeNotFound n1)
else if ((isNodeOf g n2) = false) then
raise (NodeNotFound n2)
else
let nodes = g.nodes
and edges = (n1, n2) :: g.edges in
{
nodes;
edges;
}
let nextNodes g n =
let rec findSuccessors edges n =
match edges with
[] -> []
| (n1, n2) :: t ->
if n1 = n then n2::findSuccessors t n
else findSuccessors t n
in
findSuccessors g.edges n
let rec lastNode path =
match path with
[] -> raise (NodeNotFound 0)
| n :: [] -> n
| _ :: t -> lastNode t
end
module Paths = struct
let extendPath g path =
let n = (Graph.lastNode path) in
let nextNodes = Graph.nextNodes g n in
let rec loop path nodes =
match nodes with
[] -> []
| h :: t -> (List.append path [h]) :: (loop path t)
in
loop path nextNodes
let rec extendPaths g paths =
match paths with
[] -> []
| h :: t -> List.append (extendPath g h) (extendPaths g t)
(* Given a list lst, return a new list with all duplicate entries removed *)
let rec removeDuplicates lst =
match lst with
[]
| _ :: [] -> lst
| h :: t ->
let trimmed = removeDuplicates t in
if List.mem h trimmed then trimmed
else h :: trimmed
end
Any expression can be a recursive function call. There are no limitations like that. Your problem is that some types don't match.
I don't see any ints in this code, so I'm wondering where the compiler sees the requirement for an int list. It would help to see the type definition for your graphs.
As a side comment, you almost certainly have a precedence problem with this code:
dfslsts g ["%d"] (List.flatten l)::final
The function call to dfslsts has higher precedence that the list cons operator ::, so this is parsed as:
(dfslsts g ["%d"] (List.flatten l)) :: final
You probably need to parenthesize like this:
dfslsts g ["%d"] ((List.flatten l) :: final)

What am I doing wrong with Set.Fold F#

Colouring problem :
Hello, I'm trying to implement a bool function that returns true when a color can be extended to a country and false otherwise but I'm having trouble working with sets as we cannot pattern match them...
My code :
type Country = string;;
type Chart = Set<Country*Country>;;
type Colour = Set<Country>;;
type Colouring = Set<Colour>;;
(* This is how you tell that two countries are neghbours. It requires a chart.*)
let areNeighbours ct1 ct2 chart =
Set.contains (ct1,ct2) chart || Set.contains (ct2,ct1) chart;;
(* val areNeighbours :
ct1:'a -> ct2:'a -> chart:Set<'a * 'a> -> bool when 'a : comparison
*)
(* The colour col can be extended by the country ct when they are no neighbours
according to chart.*)
let canBeExtBy (col:Colouring) (ct:Country) (chart:Chart) = col |> Set.fold (fun x -> (if (areNeighbours x ct chart) then true else false))
What is expected : we need to check if ct is a neighbour of any country in the col (assuming there are countries in the col), according to the neighbourhood defined in the chart.
So if
chart = set
[("Andorra", "Benin"); ("Andorra", "Canada"); ("Andorra", "Denmark");
("Benin", "Canada"); ("Benin", "Denmark"); ("Canada", "Denmark");
("Estonia", "Canada"); ("Estonia", "Denmark"); ("Estonia", "Finland");
...]
And
col = set
["Andorra"]
Then canBeExt should return false when when ct = "Benin" or "Canada" or "Denmark" etc... As Andorra share a border with those countries and thus they cannot be coloured in the same colour as Andora.
Obviously I have a type error in canBeExtBy as I'm trying to return a bool and it's expecting 'a:Colouring.
I don't know how to implement it..
Thanks for your help !
What about this?
type Country = string
type Neighbours = Set<Country*Country>
type SharesColour = Set<Country>
let areNeighbours (ns : Neighbours) (ct1 : Country) (ct2 : Country) : bool =
Set.contains (ct1,ct2) ns || Set.contains (ct2,ct1) ns
let canShareColour (ns : Neighbours) (ct : Country) (s : SharesColour) : bool =
s |> Seq.exists (areNeighbours ns ct) |> not
let neighbours : Neighbours =
set [|
("Andorra", "Benin") ; ("Andorra", "Canada") ; ("Andorra", "Denmark");
("Benin" , "Canada") ; ("Benin" , "Denmark"); ("Canada" , "Denmark");
("Estonia", "Canada") ; ("Estonia", "Denmark"); ("Estonia", "Finland");
|]
let sharesColour : SharesColour =
set [|
"Andorra"
|]
[<EntryPoint>]
let main argv =
printfn "%A" <| canShareColour neighbours "Estonia" sharesColour
printfn "%A" <| canShareColour neighbours "Benin" sharesColour
0
Changed the names to something that made more sense to me. You might not agree.
To check that no element in a collection satisfies a given condition: that's not a job for fold, but, with appropriate negation, for exists or forall.
Either of
let fornone f = Set.forall (f >> not)
let doesnotexist f = Set.exists f >> not
will do, as shown in the answer of FuleSnabel. However, it is certainly possible to build these functions from a fold, albeit nobody would do that except as an illustration of currying, function composition and pointfree style.
let fornone f = Set.fold (fun s -> f >> not >> (&&) s) true
let doesnotexist f = Set.fold (fun s -> f >> (||) s) false >> not

Elm: How to use Json

I do not know how to use Json.decode.
type alias Test =
{ a : Int
, b : Int
}
testDecoder =
object2 Test
("a" := int)
("b" := int)
main : Html
main =
let
t = "{\"a\":2, \"b\":2}"
d = decodeString testDecoder t
in
p [] [ text <| toString <| d ]
I want to get value of "a".
I do not know "Ok { a = 2, b = 2 }".
decodeString : Decoder a -> String -> Result String a
Since decodeString returns a Result String a, it could either be an error or success result. You have to do a case statement and look for Ok and Err, like so:
main : Html
main =
let
t = "{\"a\":2, \"b\":2}"
d = decodeString testDecoder t
myText =
case d of
Ok x -> toString x.a
Err msg -> msg
in
p [] [ text myText ]

OCaml function typing issue

I'm trying to write a Caml function and I'm having a few troubles with the typing. The function is :
let new_game size count gens =
let rec continueGame board = function
0 -> ()
|n -> drawBoard board size;
continueGame (nextGeneration board) (n-1)
in
continueGame (seedLife (matrix 0 size) count) (gens) ;;
Here are the types of other functions :
val drawBoard : int list list -> int -> unit = <fun>
val seedLife : int list list -> int -> int -> int list list = <fun>
val nextGeneration : int list list -> int list list = <fun>
val matrix : 'a -> int -> 'a list list = <fun>
When trying to evaluate new_Game I have the following error :
continueGame (seedLife (matrix 0 size) count) (gens);;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This expression has type int -> int list list
but is here used with type int list list
Why is this error occuring and how can I resolve it?
seedLife takes 3 arguments, but it's only passed 2.