How to render list in elm? - list

I have a hard time to render List of custom types within the view function.
This is the model:
type alias Guid = String
type alias User = String
type alias TaxonomyCategory =
{ id : Guid
, name: String
, updatedAt: Date
, updatedBy: User
, terms: List TaxonomyTerm
}
type TaxonomyTerm =
TaxonomyTerm
{ id : Guid
, name: String
, terms: List TaxonomyTerm
}
I tried several approaches with List.map function but I always ended up with some kind of error message.
The 2nd argument to function `ul` is causing a mismatch.
120| ul
121| []
122|> [ List.map renderTaxonomyTerm tc.terms ]
Function `ul` is expecting the 2nd argument to be:
List (VirtualDom.Node a)
But it is:
List (List (Html a))

The second parameter of ul should be a list of Html elements. Your second value contains a list within a list since you surrounded it with brackets. Changing it to this should fix your problem:
ul
[]
(List.map renderTaxonomyTerm tc.terms)

For using ul see previous answer. Thanks to Chad.
I have an addition. You can't create recursive records. You should create structure like type TaxonomyTerm = TaxonomyTerm Guid String (List TaxonomyTerm).

Related

Sml tuples length

I was interested in if there is a possible way to get length of tuple in sml?! See example
val tes = ((1,"test"),("test","some"))
Lenght(tes) = 2
I want it for a problem solve there is a problem which says to get students list which contains list for each student information but student information are different in two types some are like
(1,"test","nick")
and some are like
("name","nick")
So it wants to return the first element of each list in student lists see below:
((1,"test","nick"),("test2","nick2"),(2,"test3","nick3"))
Return > (1,"test2",2)
Here more info M Molbdnilo
#molbdnilo
An example of what you're most likely expected to do; define some useful sum types.
First, let's invent two ways to identify a person:
datatype Person = JustName of string
| NameAndNumber of string * int
datatype Identifier = Name of string
| Number of int
then you can get an Identifier for a Person:
fun identifier (JustName n) = Name n
| identifier (NameAndNumber (_, i)) = Number i
Let's test with some people:
- val people = [JustName "A", NameAndNumber ("B", 23), JustName "C", NameAndNumber ("D", 22)];
val people =
[JustName "A",NameAndNumber ("B",23),JustName "C",NameAndNumber ("D",22)]
: Person list
- map identifier people;
val it = [Name "A",Number 23,Name "C",Number 22] : Identifier list

Power Query Table.TransformRows won't return table

In the M language documentation for Table.TransformRows, it lists the type signature as Table.TransformRows(table as table, transform as function) as list
followed by the slightly cryptic comment
If the return type of the transform function is specified, then the result will be a table with that row type.
However, whenever I define my function, Power Query always returns me a list instead of a table.
For instance, this query:
func = (row) as record => [B = Number.ToText(row[a])] as record,
#"firstTable"= Table.FromRecords({
[a = 1],
[a = 2],
[a = 3],
[a = 4],
[a = 5]}),
#"myTable" = Table.TransformRows(#"firstTable",func)
in
#"myTable"
returns a table.
I had wondered if I needed to create a record type which specifies the type of each record entry, but if I try
myType = type [B = text],
func = (row) as record => [B = Number.ToText(row[a])] as myType
then it tells me that my type identifier is invalid on the second line. Finally, I tried to set the function return type using
myType = type [B = text],
func1 = (row) as record => [B = Number.ToText(row[a])] as myType,
funcType = Type.ForFunction([ReturnType = myType, Parameters = [X = type number]], 1),
func = Value.ReplaceType(func1,funcType)
but Table.TransformRows still returns a list.
I am aware that I am able to use Table.FromRecords or Table.FromRows to turn the result of Table.TransformRows into a table, but I am currently experiencing some performance issues with these functions and was trying to cut them out to see if this would fix those issues
typing inline
returns a table. I had wondered if I needed to create a record type which specifies the type of each record entry
// you had this
record_list = { [ a = 1 ], [ a = 2 ], [ a = 3 ], [ a = 4 ], [ a = 5 ] },
firstTable = Table.FromRecords(
record_list
)
Which ends up as a column of type any. To fix that:
// declare the type
firstTable = Table.FromRecords(
record_list,
type table[a = text]
)
Whenever a function takes the parameter columns as any you can use any of these formats
"Name"
`{ "Name1", "Name2" }
type table[ Name1 = text, Name2 = number ]
Invalid type Identifier
then it tells me that my type identifier is invalid on the second line
The as operator only works on primitive data types.
If it were to work, this would mean assert the record is a myType, then the function return asserts the type is record, which is mixed types. The function already has a final assert, so you don't need one on your inner calls.
func = (row) as record => [B = Number.ToText(row[a])] as something
Replacing Function Types does not change behavior
Finally, I tried to set the function return type using ... Value.ReplaceType
Using Value.ReplaceType on a function does not modify the actual types or behavior. That's just changing the metadata.
Ben Gribaudo: /part18-Custom-Type-Systems As type ascription can only change information about a function, not the function’s behavior at the language level, ascribing a type onto a function that specifies different argument or return assertions has no effect on the behavior of the function. The mashup engine always uses the type assertions specified when the function was originally defined, even if another type is later ascribed onto it.
Check out Ben's Power Query series. It's high quality. It goes into far more detail than other places.
Performance
I am aware that I am able to use Table.FromRecords or Table.FromRows to turn the result of Table.TransformRows into a table, but I am currently experiencing some performance issues
Missing column data-types can cause issues. But you really should try the query diagnostics, it will tell you what steps are taking the most time.
Based on your example, you might want transform column instead of rows.
Also make sure your query is folding!
Example using Table.TransformRows with typing
let
// sample data, list of records like: [ Number = 0 ]
numberRecords = List.Transform( {0..10}, each [Number = _] ),
Source = Table.FromRecords(
numberRecords,
type table[Number = number]
),
transform_SquareNumbers = (source as table) =>
let
rows = Table.TransformRows(
source, (row) as record =>
[
Number = Number.Power(
row[Number], 2
)
]
),
result = Table.FromRecords(rows),
source_type = Value.Type(source)
in
Value.ReplaceType(result, source_type),
FinalTable = transform_SquareNumbers(Source)
in
FinalTable

How to convert a list of strings into a dict object with kwarg as the keys?

I have seen similar questions. This one is the most similar that I've found:
Python converting a list into a dict with a value of 1 for each key
The difference is that I need the dict keys to be unique and ordered keyword arguments.
I am trying to feed the list of links I've generated through a scraper into a request command. I understand the request.get() function only takes a URL string or kwarg parameters - hence my need to pair the list of links with keyword arguments that are ordered.
terms = (input(str('type boolean HERE -->')))
zipcity = (input(str('type location HERE -->')))
search = driver.find_element_by_id('keywordsearch')
search.click()
search.send_keys('terms')
location = driver.find_element_by_id('WHERE')
location.click()
location.send_keys('zipcity')
clickSearch = driver.find_element_by_css_selector('#buttonsearch-button')
clickSearch.click()
time.sleep(5)
cv = []
cvDict = {}
bbb = driver.find_elements_by_class_name('user-name')
for plink in bbb:
cv.append(plink.find_element_by_css_selector('a').get_attribute('href'))
cvDict = {x: 1 for x in cv}
print(cvDict)
SOLVED: (for now). Somehow figured it out myself. That literally never happens. Lucky day I guess!
cvDict = {'one': cv[:1],
'tw': cv[:2],
'thr': cv[:3],
'fou': cv[:4],
'fiv': cv[:5],
'six': cv[:6],
'sev': cv[:7],
'eig': cv[:8],
'nin': cv[:9],
'ten': cv[:10],
'ele': cv[:11],
'twe': cv[:12],
'thi': cv[:13],
'fourteen': cv[:14],
'fifteen': cv[:15],
'sixteen': cv[:16],
'seventeen': cv[:17],
'eighteen': cv[:18],
'nineteen': cv[:19],
'twent': cv[:20],
}

parameters aren't being read in py2neo

I'm trying to query based on node number and return a list of properties. The following cypher query works in the neo4j browser but when I try to pass the same cypher query via py2neo I get:
"Expected a property container or number here, but got:91"
where "91" is the node number I'm querying on.
an excerpt from my code:
def neighbor_finder(a):
try:
graph_db = neo4j.GraphDatabaseService(url)
query = neo4j.CypherQuery(graph_db,
"""CYPHER 2.0
MATCH (n)-[r]-(m)
WHERE ID(n) = {t}
WITH collect (Distinct m.size) as sizes, collect (Distinct m.weight) as weights, collect (Distinct m.color) as colors, collect (distinct n.label) as node_
RETURN sizes, weights, colors, node_
""")
result = query.execute(t=a)
for r in result:
column = [str(item) for item in r.columns]
value = [str(item) for item in r.values]
db_dict={k: v for k, v in zip(column, value)}
for x, y in db_dict.items():
print x, y
except Exception as e:
print e
Can you provide details of the type and value of the a argument that is passed into this function? Without it I cannot see what py2neo is passing into the query parameters.
argument "a" isn't being read because it needs to be declared as an integer, cypher/py2neo are currently reading it as a string.

Html agility pack tag selection

I need to process list tags in order to extract data from them. The problem is that I need to analyze each list separably. I tried something like this:
List<HtmlAgilityPack.HtmlNode> tl = new List<HtmlNode (doc1.DocumentNode.SelectNodes("//ul"));
I was expecting that every tl element will be separate ul list, but it turns out that tl has only one element containing all li tags in html document. What am I doing wrong?
I've solved the problem with following code:
foreach (HtmlAgilityPack.HtmlNode node in tk)
{
if (node.ParentNode.Name == "ul" || node.ParentNode.Name == "ol")
{
List<string> sh=new List<string>();
var t = node.ParentNode.Elements("li");
for(int i=0;i <t.Count();i++)
sh.Add(t.ElementAt(i).InnerText);
uoList.Add(sh);
}
}
Now every uoList list member represents an ul or ol element which contains all li element's inside that element.