How to pattern match a function? - smlnj

H,
I am trying to do pattern matching, but the input to the function is a curried function, how can you pattern match something like that?
Can anyone show me some examples please?

If I understand you correctly, then you are trying to pattern match the function you get as input?
That is not possible. A function doesn't have a structure so it doesn't really make sense to try and do that.

Related

calling anonymous function with underscore notation

I'm practicing the tricks with _ notations in anonymous functions in scala.
I have this code (beginning isn't relevant for our case):
.map(some_data => SomeObj.do_some(some_data.some_field))
And i'm wondering if i can use _ underscore notation here,
do something like:
.map(SomeObj.do_some(_.some_field)) # it doesn't work..
Thanks in advance
Your attempted syntax would be equivalent to .map(SomeObj.do_some(some_data => some_data.some_field)). Scala wouldn't know where to "insert" the parameter, where to wrap the expression in a function: it always does it at the innermost level.
You can use only once (anonymous-function) per partial function. But when they are recursively called, it is ambiguous to know where to place _. Hence you get error.

comparing strings by lexicographical order in e/specman

Does specman have something like lex_lt(s1,s2) methods? (i.e. compare strings by lexicographical order). If not, is there a recommended way to achieve the same?
It seems that there isn't. You can do 2 things here. You can either implement your own strcmp() style function in e and use that directly, or you can integrate Specman with a C file that wraps strcmp() in function that can be called from your e code. Have a look at the Specman Integrator's Guide section in the product manual for details on how to do this.
As far as I know, we don’t have something pre-defined for this.
But it can be done, for example, in the following ugly way:
if {s1;s2}.sort(it)[0] == s1 …. // if it’s TRUE, then s1 is less that s2, otherwise not
Of course, as Tudor suggested, the best way will be to define C routine to wrap strcmp().

Using a parameter value as a CDate() function input

I have a parameter {?Calendar Year} that takes on year values (2014,2015, etc.) that I would like to use in a formula. I would like it to function how one might think this would work:
{table.date}>CDate({?Calendar Year},01,01).
Does anyone know how I can accomplish this?

How can I make the value of an expression equal to a second return value of another expression

Is there an idiomatic way in Matlab to bind the value of an expression to the nth return value of another expression?
For example, say I want an array of indices corresponding to the maximum value of a number of vectors stored in a cell array. I can do that by
function I = max_index(varargin)
[~,I]=max(varargin{:});
cellfun(#max_index, my_data);
But this requires one to define a function (max_index) specific for each case one wants to select a particular return value in an expression. I can of course define a generic function that does what I want:
function y = nth_return(n,fun,varargin)
[vals{1:n}] = fun(varargin{:});
y = vals{n};
And call it like:
cellfun(#(x) nth_return(2,#max,x), my_data)
Adding such functions, however, makes code snippets less portable and harder to understand. Is there an idiomatic to achieve the same result without having to rely on the custom nth_return function?
This is as far as I know not possible in another way as with the solutions you mention. So just use the syntax:
[~,I]=max(var);
Or indeed create an extra function. But I would also suggest against this. Just write the extra line of code, in case you want to use the output in another function. I found two earlier questions on stackoverflow, which adress the same topic, and seem to confirm that this is not possible.
Skipping outputs with anonymous function in MATLAB
How to elegantly ignore some return values of a MATLAB function?
The reason why the ~ operator was added to MATLAB some versions ago was to prevent you from saving variables you do not need. If there would be a syntax like the one you are searching for, this would not have been necessary.

Method chaining with nested method

I am new to coffeescript. However I didn't find any suitable word to put the question.
I have a coffeescript like this :
#collection.each (student) =>
($(#el).find("#table .table").append new Item({model:student}).el)
.find("td:last-child").hide()
However, is there any better way to do this method chaining than this ugly syntax ? I want to find the td:last-child from $(#el) only, without any brackets. How can I do that?
Why not put the parentheses on the append to match the other function calls?
#collection.each (student) =>
$(#el).find("#table .table")
.append(new Item(model: student).el)
.find("td:last-child")
.hide()