Can't get record property of a record - record

I have this
{ a |
b = { a.b |
c =
Utils.newC
a.b.c
}
}
But the compiler just says "no" :
-- SYNTAX PROBLEM ----------------------------------------------------- Main.elm
I ran into something unexpected when parsing your code!
43| b = { a.b |
^
I am looking for one of the following things:
"'"
"|"
an equals sign '='
more letters in this name
whitespace
I don't know what to do now. How to get a with c property of b changed to a new value?

Updating nested records is a little more verbose in Elm than in other languages, and the syntax of { a.b | ... } updates is not allowed. Here is an alternative:
let
b = a.b
newB = { b | c = Utils.newC b.c }
in
{ a | b = newB }
See this related question for more information on standard ways of updating nested record values in Elm.

Related

Sum up the elements of a record

type MyType = {a:int; b:int};;
let test = [{a=5;b=10}; {a=10;b=100}; {a=200; b=500}; {a=100; b=2}];;
I would like to create a function which will sum up a and b in such a way it will display
Sum of a : 315
Sum of b : 612
I think I have to use a recursive function. Here is my attempt :
let my_func record =
let adds = [] in
let rec add_func = match adds with
| [] -> record.a::adds; record.b::adds
| _ -> s + add_func(e::r)
add_func test;;
This function doesn't seem to work at all. Is there a right way to do such function?
OK, you know exactly what you're doing, you just need one suggestion I think.
Assume your function is named sumab and returns a pair of ints. Then the key expression in your recursive function will be something like this:
| { a = ha; b = hb; } :: t ->
let (a, b) = sumab t in (ha + a, hb + b)

Update Elm record with multiple constructors

Is there a way to modify field for a type with multiple constructors?
type U = S | C { xxx : String }
This is not a record, so it has no fields to update!
5| let c = C { xxx = "DD"} in case c of C {} -> { c | xxx = "ZZ" }
^
This `c` value is a:
U
But I need a record!
In type U = S | C { xxx : String }, xxx is not a field of U. It is a field of the record contained in C. Those are separate types that can be deconstructed into separate values. And the way to do that is to use case to match the C constructor and bind the contained value to a name (or further deconstruct it) so we can refer to it on the right side of the ->.
But then you also need to handle the possibility of c being S. What should be returned then? Maybe a default? Or maybe you actually want to return a U? I've assumed the former here, but the latter would just be construction values of U as you would anywhere else.
let
c =
C { xxx = "DD" }
in
case c of
C record ->
{ record | xxx = "ZZ" }
S ->
-- What to do here?
{ xxx = "default?" }

What is the most idiomatic way of representing errors in F#

I'm working on F# project and I wonder what is the best practice to return domain error using Result type in F#. There are several ways of doing it which I consider:
Inherited exceptions
type DomainException(message) =
inherit Exception(message)
type ItemNotFoundException(item) =
inherit DomainException(sprintf "Item %s is not found" item)
let findItem item =
match item with
| Some x -> Ok x
| None -> Error(new ItemNotFoundException("someitem"))
Custom record type
type DomainError =
{ Name : string
Message : string }
let findItem item =
match item with
| Some x -> Ok x
| None ->
Error({ Name = "ItemNotFound"
Message = "Item someitem is not found" })
Discriminated union of record type
type DomainErrorTypes =
| ItemNotFoundError of DomainError
| ItemInvalidFormat of DomainError
let findItem item =
match item with
| Some x -> Ok x
| None ->
{ Name = "ItemNotFound"
Message = "Item someitem is not found" }
|> ItemNotFoundError
|> Error
So which way is more idiomatic and convenient to use? I also will be happy to see better options.
Typically it would be a discriminated union. Every error requires different details to accompany the message. For instance:
type DomainErrorTypes =
| ItemNotFound of ItemId
| FileNotFound of string
| InvalidFormat of format
| IncompatibleItems of Item * Item
| SQLError of code:int * message:string
| ...
You can also capture some exceptions (not necessarily all):
| ...
| Exception of exn

Is it possible to use Maybe with Extensible Records?

I am trying to write a function to pull the maybe off a list of extensible records, I am wondering if this is possible. Source code is below, or see Ellie link here
module Temp exposing (..)
import Html exposing (text)
main =
text "Hello"
items : Maybe List { data | id : Int } -> List { data | id : Int }
items maybeList =
case maybeList of
Just t ->
t
Nothing ->
[]
Maybe List { data | id : Int } parses as Maybe (List) ({ data | id : Int }). I'm not sure why the error message is so misleading, but the fix is to wrap List ... in () like this:
items : Maybe (List { data | id : Int }) -> List { data | id : Int }
^ ^
Edit: also, your function can be simplified using Maybe.withDefault:
items = Maybe.withDefault []

Issue utilizing OrderBy With Neo4jClient

I'm sorting a large database by the quantity of outgoing relationships. I have a working Cypher query as follows:
optional match (n)-[r]->(m)
return n, Count(r) as c
order by c DESC limit 1;
The Cypher query is working as anticipated. However, as I am debugging and stepping through the Cypher -> Neo4jClient conversion, I cannot seem to find the root of the issue.
public ReturnPayload[] getByConnections()
{
var query = client.Cypher
.OptionalMatch("(p)-[r]->(m)")
.Return((p, r, m, c) => new
{
p = p.As<Person>(),
pid = (int)p.Id(),
e = r.As<RelationshipInstance<Object>>(),
m = m.As<Metadata>(),
c = r.Count()
}).OrderByDescending("c").Limit(1);
var res = query.Results;
var payload = new List<ReturnPayload>();
foreach (var el in res)
{
var t = new ReturnPayload();
t.e = el.e;
t.m = el.m;
t.pid = el.pid;
t.p = el.p;
payload.Add(t);
}
return payload.ToArray<ReturnPayload>();
}
I suspect that a part of the problem may be that I am not utilizing CollectAs<T>() and thus it is referring a count of '1' per each person. Unfortunately, I have attempted using CollectAs<T>() and CollectAsDisctinct<T>() and my resultant JSON architecture is only wrapping each individual element in an array, as opposed to aggregating identical elements into an array proper.
The FOREACH loop is there to assist in converting from the anonymous type into my relatively standard <ReturnPayload> which does not utilize a c object within its parameters.
Your time is appreciated, thank you.
Debugged Query Test:
OPTIONAL MATCH (p)-[r]->(m)
RETURN p AS p, id(p) AS pid, r AS e, m AS m, count(r) AS c
ORDER BY c DESC
LIMIT {p0}
And my 'functional' Cypher query:
optional match (n)-[r]->(m)
return n, Count(r) as c
order by c DESC limit 1;
So, you've identified yourself that you're running two different queries. This is the issue: your C# does not match the Cypher you're expecting.
To make the C# match the working Cypher, change it to this:
var query = client.Cypher
.OptionalMatch("(n)-[r]->(m)")
.Return((n, r) => new
{
n = n.As<Person>(),
c = r.Count()
})
.OrderByDescending("c")
.Limit(1);
That should now produce the same query, and thus the same output as you're expecting from the working Cypher.
On a purely stylistic note, you can also simplify your foreach query to a more functional equivalent:
var payload = query
.Results
.Select(r => new ReturnPayload {
n = r.n,
c = r.c
})
.ToArray();
return payload;
However, as I look closer as your query, it looks like you just want the count for the sake of getting the top 1, then you're throwing it away.
Consider using the WITH clause:
optional match (n)-[r]->(m)
with n as n, count(r) as c
order by c desc
limit 1
return n
You can map that back to C# using similar syntax:
var query = client.Cypher
.OptionalMatch("(n)-[r]->(m)")
.With((n, r) => new
{
n = n.As<Person>(),
c = r.Count()
})
.OrderByDescending("c")
.Limit(1)
.Return(n => new ReturnPayload // <-- Introduce the type here too
{
n = n.As<Person>()
});
Then, you don't need to query for data and just throw it away with another foreach loop. (You'll notice I introduce the DTO type in the Return call as well, so you don't have to translate it out of the anonymous type either.)
(Disclaimer: I'm just typing all of this C# straight into the answer; I haven't double checked the compilation, so my signatures might be slightly off.)
Hope that helps!