Break from within a oneliner - coffeescript

Is it possible to simplify the second line keeping the code in three lines at the same time? current should have the "error" value.
for item in [ 1, 2, 3, undefined, 5, 6]
break if (current = if item? then item else "error") is "error"
console.dir current
I was trying to make something like that with no luck:
for item in [ 1, 2, 3, undefined, 5, 6]
current = if item? then item else "error", break
console.dir current

Here is a one liner, that's the best way I found to write it in coffee, but wouldn't in production, so my colleagues won't kill me while reading it.
break for item in [ 1, 2, 3, undefined, 5, 6] when (current = item ? 'error') is 'error'
console.dir current
There are actually many way to write this, with exactly the same output in Javascript.
Regards,

Related

How can I sorting ListView by trailing value in Flutter?

I have a ListView in Flutter shows stores location information from firebase after that a function works to calculate a distance between customer location and stores locations and show distance value in the trailing of ListView, now I want to sort this ListView based on trailing value (nearest stores):
Most of solution I have tried was so complicated and want an easy a effective way.
For ascending
var dataList = [1, 6, 8, 2, 16, 0] //your dynamic list
dataList.sort((a, b) => a.compareTo(b));
For descending
var dataList = [1, 6, 8, 2, 16, 0] //your dynamic list
dataList .sort((b, a) => a.compareTo(b));
don't forgot to call setState to reload your UI again

Modify Flutter CupertinoPicker scroll to not continuously scroll?

Is there a way to modify CupertinoPicker scroll so that it's not possible to continuously scroll through a range. For example, right now if the range of values is 1-7, a user is able to continuously scroll 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, ... Is there a way to modify CupertinoPicker to have the scroll stop at the 1 and 7 (in this use case), so that there isn't a continuous scroll?
Update: found it; there's a property called "looping" (boolean)

Changing icon offset based on zoom level

I am trying to offset symbols of a symbol layer so that they don't interfere with a previous symbol layer (i.e. they don't overlap). I need to offset them as in both cases icon-allow-overlap needs to be set to true, as the symbols need to be viewable at all zoom levels. Ideally I'd like to do something like this:
"icon-offset": [
["zoom"],
12, [-16, 0],
22, [0, 0]
]
but that gives me an error:
array length 2 expected, length 5 found
Is there a way I can do what I want similar to what I was trying above? I know that icon-offset is not transitionable so that is why the above is failing.
Any help would be appreciated.
Thanks for your time.
The answer was to use a function:
"icon-offset": {
"stops": [
[12, [-16, 0]],
[22, [0, 0]]
]
}
More info on this can be found here

Given two sets, how can I determine what was added or removed?

let first = { 2, 4, 5, 9 };
let second = { 2, 8, 15, 53, 4 }
//removed = 5, 9
//added = 8, 15, 53
What is the simplest solution to determine what was added or removed?
Just use set differences.
To determine what was added, take the set subtraction
second.subtracting(first)
To determine what was removed, take
first.subtracting(second)
A very simple algorithm for determining the changes is similar to the merge algorithm for sorted lists:
Order both sets
Start iterating both ordered lists at the initial element
In a loop, if the current item from both lists is the same, skip it by moving to the next element of both lists
If the item in the original set is smaller, add it to the list of removed items, and move the original set to the next item
If the item in the new set is smaller, add it to the list of added items, and move the new set to the next item
If you reach the end of one of two lists before the other one, add the remaining elements to the corresponding output list (added or removed).

How do I sort lines semi-lexiographically in emacs -- i.e., lexiographically, except that 3 gets sorted above 11?

How do I sort lines semi-lexiographically in emacs -- i.e., lexiographically, except that 3 gets sorted above 11? For example, I have a large collection of data, each entry of which looks like
[ 5, 3, 21, 1600000 ],
[ 3, 11, 21, 6400000 ],
[ 3, 3, 102, 1600000 ],
etc...
M-x sort-lines sorts this as
[ 3, 11, 21, 6400000 ],
[ 3, 3, 102, 1600000 ],
[ 5, 3, 21, 1600000 ],
but I would really like this sorted as
[ 3, 3, 102, 1600000 ],
[ 3, 11, 21, 6400000 ],
[ 5, 3, 21, 1600000 ],
Thanks!
sehe gives a good solution. Here it is in Emacs:
C-u M-| sort -k2n -k3n
Run that with your region selected and it will be replaced with the sort ouput!
I don't use emacs, but in vim I'd do:
%!sort -k2n -k3n
(possibly using the other key columns as well, I can't tell form the sample)
I'm not starting the editor war here... I'm just pretty sure that emacs allows you to filter through a shell command as well, so this will help!
With your data, the following will do what you want, though it is a bit laborious:
C-u 3 M-x sort-numeric-fields
C-u 2 M-x sort-numeric-fields
I don't know for sure that sort-numeric-fields is a stable sort, so it may not always work. And, obviously the above only sorts 2 numbers "deep" and you'll need to add C-u 4 M-x sort... if you want to sort by the 3rd number. The prefix argument starts with 2 because the first field is the [, and counting begins with 1.
You could also roll your own by calling sort-subr with the appropriate, lexographic, predicate. See the documentation for sort-subr for more details.