Map and mapcon available

Hi there!

I just coded two nice filter operators today: first of them is map (which I derived heavyly from the code of the filter filter operator) and then mapcond which extends map in a IMHO very useful way: this is a map with condition (as a function of the item) and depending on it, map for each item either on a then-function or on else-function. Applied to singeleton, this is also an if-then-else filter.

example:

<$vars test-if-greater-than-20="[compare:integer:gt[20]]" add-3="[add[3]]" minus-7="[subtract[7]]">
{{{ [enlist[12 17 5 28 64]mapcond<test-if-greater-than-20>,<add-3>,<minus-7>] }}}
</$vars>

Would people be interested?

1 Like

I think you can do something similar with just the core filter affordances, untested code follows:

[enlist[12 17 5 28 64]] :map[compare:integer:gt[20]then[3]else[-7]add<currentTiddler>]

We probably should add map[] operator to the core to compliment the filter run prefix.

My example was quite too simple. I have real life example where is is not so trivial.

Beside, my original idea was a way to transform [a] into [a] [f(a)] where f(a) is the result of a filter applied to a. Contrete case : a is an index and f(A) the index value, and I wanted to forge a message exposing the location of the error (a) and the value causing the error (f(a)). I was able to get [a] [a] from [a] and tgen the mapcond was the final stroke.

any idea to make otherwise is welcomed.

map should really integrate the core. it’s really simple to get it from filter (2min of work!).

A PR to add a map filter operator to the core would be very welcome. We would likely want to support the same variables within the subfilter as reduce[] (except the accumulator variable)

I’m going to have a look at the code of reduce then, for so far I have no variable.

I have checked the code and yes the support of currentTiddler, ..currentTiddler and other variables was already within my code. I was just not knowing exactly what this code does.

However, it is not really a map because its output doesn’t necessarily have the same number of items than its input. It’s mor like a transformative filter that would only output the non empty outputs after calling the function for each input. This is also very handy a tool, but a proper map filter it is not.

Whatever; here is that map.js.
map.js.json (1.1 KB)

I think the map is being improved in the pre-release however I recall it makes the filter designer handle the empty case, so that the out put has the number of items as the input. Who knows there may be an interesting algoritium created from the misuse of MAP

Indeed my special kind of map is very interesting for filters.

And I can also tell you that I have devised a complex filter run that, in spite of being prefixed with :map’ has a number of outputs values lesser than the number on input values! This was not intended as to being so, but just for doing needed stuff done, and I perceived that difference only later.

I wonder if this is an opportunity to collapse a set of parameters for use in macros?

<<macroname p1 p2 p3 p4 p5>> such that inside macroname we evaluate each parameter and make use of only those that produce a result. Effectively allowing the macro to process a subset of parameters eg “p1 p3 and p5” as 1st, 2nd and 3rd (because they return a value but p2 and P4 do not),

@TW_Tones I am not seing what’s your purpse is. macro parameters are generally meant to be precise things, so ok that would fit only some kind of macros. but so what?

puzzled I am.

1 Like

@jypre I always maintain watching brief on possible hacks. Especially when there is a suggestion “if you do not do it this way it will not work”. Often because it does not do as intended, yet new possibilities arise. If nothing else I get to observe the outcome if I have coded in error.

Software may seem to be precise, yet it almost Always is not, the main argument for this is the persistence of bugs. But unexpected side effects are not always unwanted.

The value of hacks is especially true when a desirable feature is missing.

In this case I speculate if this may solve the following issue -

If I call a <<macro "p1" "p2">> without naming the parameters they are assigned as ordered in the macro definition, but they can not exceed the number of parameters defined therein, if you use the macrocall widget each parameter must be named. If you wan to supply a variable number of parameters there is no way to reference all parameter’s provided unless they are defined before hand. The exception is using a filter, that resolves to multiple values/parameters as the value for “one of the parameter’s”.

This started for me in High School > 1982 when I/we would do out best to find undocumented functions on our calculators. One very rewarding discovery was my calculator had almost all the functions of a more expensive model, they were just not labeled.

@TW_Tones This filter as parameter idea is indeed a nice trick to know in case the need arise. How clever of you!

And what was your calculator then? I’d be curious to know.

1 Like

Casio Scientific, Exact model eludes me but from ancient neurons fx1000 but I thought it was a dark colour, but possible completely wrong.

fx1000 is really old stuff. I did not see any of such early casio at that time. There was a time for ti and hp… and other were oddities. a few olympia of poor interest perhaps.