Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
For example the input
["a", "b", "b", "c", "a"]
would lead to the output
[("a", 2), ("b", 2), ("c", 1)]
I can't really think of a functional way to do this in elm
Using existing code is a great idea, but I think it also makes sense to see the concepts:
To solve the requirement, you traverse the list using a recursive function and build an intermediary data structure. In this case a dictionary because it fits well to counting the occurrences of a string.
Then after the list was traversed and all elements were counted, you transform it to the list of tuples.
Full code on https://ellie-app.com/cLBnWHSBj5ta1
gather : List comparable -> Dict comparable Int -> List ( comparable, Int )
gather list dict =
case list of
[] ->
Dict.toList dict
first :: rest ->
let
count =
case Dict.get first dict of
Just value ->
value + 1
Nothing ->
1
in
Dict.insert first count dict
|> gather rest
Most people like to use fold instead of case-ing on the list, the ellie example also contains that code.
But the approach is the same: Solve the trivial case first (empty list) and then recurse the function until you meet the trivial case.
If you can use List.Extra.gatherEquals then you can just map the result of that function to fit your needs:
import List.Extra
List.map (\(x, y) -> (x, 1 + List.length y)) (List.Extra.gatherEquals ["a", "b", "b", "c", "a"])
-- [("a",2),("b",2),("c",1)]
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Page 109 of The AWK Programming Language book has a statement to create a 2-D array named attr:
attr[nrel, $1] = ++nattr[nrel]
nrel = an integer representing the number of relations (tables)
nattr = an integer representing the number of attributes (table columns)
Substituting country for $1, 1 for nrel, and 1 for nattr we have:
attr[1, country] = 1[1]
What does the right-hand side of that statement mean? It appears to be referencing subscript 1 of array 1. Can an array be named 1? Would you explain what that expression means, please?
Page 109(...)
I have look into archive.org's version and line
attr[nrel, $1] = ++nattr[nrel]
is sole line referencing nattr and therefore this is where nattr is created, as you are asking about value under key it will be array. You might check that by substituting all but nattr as proposed and using typeof function
awk 'BEGIN{attr[1, "France"] = ++nattr[1];print typeof(nattr)}' emptyfile
gives output
array
therefore shown line does firstly increase value in array nattr under key nrel and then assign such changed value to array attr under key nrel, $1.
(tested in gawk 4.2.1)
I have a List and I basically want to zip it, but only every second entry.
What I mean is: I want my List [a,1,b,2] to become [(a,1),(b,2)],
I currently use zipWith.
But it does not give me the expected result, it gives me [(a,1),(1,b),(b,2)].
Am I being completely stupid right now, or is there no other solution than just ignore every second tuple? (e.g. by adding a filter afterwards) Is there no operator for that?
The chunked function in Kotlin 1.2 does exactly what you need:
val list = listOf("a", 1, "b", 2)
val newList = list.chunked(2) // returns listOf(listOf("a", 1), listOf("b", 2))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have my excel sheet data(i converted into array format) which looks like following
1st row......['one', , , , 'Folder', 'Folder', 'Extended Data', 'Extended Data', 'Extended Data','Extended Data' ],
2nd row.....['ID', 'Label', 'Longitude', 'Latitude', 'Country', 'City', 'Inventory', 'Safety stock', 'weight', 'hdsjka'],
3rd row......['AFKBL', 'Kabul, Afghanistan', 69.136749, 34.53091, 'Afghanistan', 'Kabul', 12, 1845, 12, 1845],
4th row......['AFKDH', 'Kandahar, Afghanistan', 65.700279, 31.61087, 'Afghanistan', 'Kandahar', 18, 1193, 18, 1193], ....etc etc
I want to pull all the values in the 2nd row that comes under 'Extended Data' ( which is in 1st row)
and write it into a single column array in a different file..
I want to use this column array for creating a control wrapper in google charts.
I would really appreciate if anybody could write a macro and help me on this..
I can't quite make out how what your array looks like, but it seems to me that the data you want should be simple to obtain by looping across the array cells.
Say the data you want is in row 2 and columns 4 to 7 of the array you already have ("oldarr"), then you just create a new array of newarr(4,1).
dim newarr(4,1)
for j = 1 to 4
newarr(j,1) = arr(2, (j+3)) ''cycles across the needed columns on the second row
next j
You can then paste the contents of newarr wherever you like.
Now, this seems much too simple to require a macro to do, which is why I think I have to be missing something. However, the general approach holds as long as you know which array columns will contain the information you want. The only subtleties I could think of would be if you don't know how many rows or columns you need to copy in each iteration (in which case you could use a dynamic array), or if the columns containing "Extended Data" can change.
Hope this can at least help you get started.
How can I remove specific elements from a list(=multi-valued-attribute) using a map? For example, let's say I want to filter out all b's in a given list:
<["a", "b", "c", "b"]: {<table.(it)>}; separator=",">
table ::= ["b":, default: key]
The desired outcome would be "a,c" but the actual result is "a,,c,"
The thing is that the map successfully turn b's into nulls, but then they're wrapped in an anonymous template {} and become non-null values. So they won't go away with strip() function, either.
So the question is, would it be possible to filter a list using a map by slightly modifying the code above?
update
I've found a workaround:
filter(it) ::= "<if(it)><it><endif>"
<["a", "b", "c", "b"]: {<table.(it)>}: filter(); separator=",">
This gives the result I wanted: a,c
Might not want to filter in your template, but nonetheless, could be a bug.
Ok, i checked it out. That gives empty not null so it thinks it's an item. ST treats false conditionals same way: empty not null. I think you need to filter in model.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Which computer languages will allow code that dynamically extracts a variable name from a string, like user types in an argument "hhh", and the code then knows to reference a variable with the identifier: hhh? Thanks for your help.
Not exactly "dynamic variable creation", but you can get pretty much the same effect by using an associative array (a.k.a. dictionary or map). For example, in Python:
vars = {}
vars['x'] = 'hello'
vars['y'] = 10
With the above code, the keys 'x' and 'y' in the dictionary are like dynamic variables for all practical purposes, for example:
print vars['x']
> hello
vars['y'] + 6
> 16
As a matter of fact, under the hood many programming languages (Python, JavaScript, etc.) use a dictionary of bindings for implementing variables and scoping rules.
The associative array is probably a better idea to use, but just in case, php supports this without putting the code inside an eval function:
<?php
class ff {
var $u = "aagg";
}
$y = new ff();
$i = "u";
echo $y->$i;
?>