3
$\begingroup$

The following code selects a special element from a list

thelist = {"this", "notthis"};

Select[thelist, (# == "this") &]

What I want is a function which does just that and which takes lists (like thelist) and a string (like "this") as argument. However I have a problem, because Select already uses an abstract function and so the naive idea

sel:=Select[#1, (# == #2) &]

sel[thelist,"this"]

doesn't work. This is really about the Function command and the problem might not just appear with Select but any abstract function nesting.

How to construct the object I need?

$\endgroup$
1
  • 3
    $\begingroup$ Something like : Function[{x, y}, Select[x, # == y &]][thelist, "this"] ? $\endgroup$ Commented Jul 21, 2012 at 19:11

3 Answers 3

4
$\begingroup$

You could use With to assign your string to a named variable and modify it as:

With[{str = #2}, Select[#, # == str &]] &[thelist, "this"]
(* {"this"} *)

Another option is to use a formal symbol in the inner pure function and replace it with your actual string

Select[#, (# == \[FormalS]) & /. \[FormalS] -> #2] &[thelist, "this"]
(* {"this"} *)

Alternately, if you wanted to do it solely with pure functions, you could do something like what b.gatessucks mentioned in the comments.

Function[{x, y}, Select[x, # == y &]][thelist, "this"]
(* {"this"} *)
$\endgroup$
3
  • $\begingroup$ With is exactly what I was going to suggest, +1. :P $\endgroup$
    – rcollyer
    Commented Jul 21, 2012 at 20:01
  • $\begingroup$ Thanks, not using # and doing it like username b.gatessucks said in the comment was sufficient. $\endgroup$
    – Nikolaj-K
    Commented Jul 21, 2012 at 22:55
  • $\begingroup$ As another possible permutation of the code: Select[#1, Function[test, test == #2]] &[thelist, "this"]. $\endgroup$ Commented Jul 22, 2012 at 2:43
1
$\begingroup$

This is normally done with DownValues definitions:

thelist = {"this", "notthis"};

sel[L_List, S_String] := Select[L, (# == S) &]

sel[thelist, "this"]
{"this"}
$\endgroup$
1
  • $\begingroup$ Well, ... very true. I guess I was too much into a certain style of coding. $\endgroup$
    – Nikolaj-K
    Commented Jul 21, 2012 at 23:19
0
$\begingroup$

An alternative would be to use the Nearest[ ] function. This doesn't address the general issue, but does solve the specific question:

thelist = {"this", "notthis"};
Nearest[thelist, "this"]
$\endgroup$
2
  • 2
    $\begingroup$ thelist = {"cat", "but", "hit"}; Nearest[thelist, "fat"]... The example was just for illustration purposes, so merely addressing only the highly simplified case isn't very useful, IMO. $\endgroup$
    – rm -rf
    Commented Jul 21, 2012 at 21:19
  • $\begingroup$ user, I agree with R.M in this case, but nevertheless welcome to the site and thanks for contributing. $\endgroup$
    – Mr.Wizard
    Commented Jul 21, 2012 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.