Chained-functional notation examples?

85 views
Skip to first unread message

Unknown

unread,
May 6, 2014, 2:26:46 AM5/6/14
to
I came to 'Mathematica' via Xahlee's criticism of the ad-hoc nature of
unix-piping [functional notation]. He claims [& I believe him] that Mathematica has a better, more consistent notation. But the facility of PRE, IN & POST-fix alternatives, seems bad. You want ONE way of acieving the goal. More rules just increases mental load.

A major benefit of functional [unix-piping] programming style, is that you
don't need to remember the-full-journey: you just need to remember the previous stage's output. Nor do you need to remember several names: the previous output is just "it". A superficial read through a recent article[s] here, about <collecting data from several servers, and agregating it, and sending the result to a master> seemed very interesting, and matches my ideas of using functional programming. But I can't afford to invest in ANOTHER notation/syntax, without good prospect of productivety increase.

Just as a test, how would Mathematica handle the following [or part of]
little task:

search all files in Dir=tree D |
which are less than N days-old |
and which contain string S1 |
and which contain string S2 .

Actually, this seems not a good example, since it's biased towards the *nix
file system's format/syntax.

Try:
Search in table of ListOfOpenFiles for lineS with path-P [field] |
which have same tty-field as line with path-P2 & program-M [field]

This sounds like a data-base problem?

Or is there a nice list of 'such' Mathematica examples?

Thanks,

== John Grant.

Bill Rowe

unread,
May 7, 2014, 2:44:45 AM5/7/14
to
On 5/6/14 at 2:26 AM, d...@gmail.com (Unknown) wrote:

>I came to 'Mathematica' via Xahlee's criticism of the ad-hoc nature
>of unix-piping [functional notation]. He claims [& I believe him]
>that Mathematica has a better, more consistent notation. But the
>facility of PRE, IN & POST-fix alternatives, seems bad. You want
>ONE way of acieving the goal. More rules just increases mental load.

If you are looking for a system with one way to achieve a given
goal, Mathematica isn't it. There are multiple ways of achieving
a result without using different notations. For example,
consider how you might find the sum of the first n integers. All
of the following will work:

limit = 10^6;
For[sum = 0; n = 1, n <= 10, n++, sum += n]; sum

Plus@@Range[limit]

Total[Range@limit]

Sum[n,{n,m}]/.m->limit

Intelligent use of pre-, post- and in-fix notations generally
makes Mathematica code easier to read/understand. For example, I
could have written

Total[Range@limit] as

Total[Range[limit]]

but fewer levels of [] makes it easier from my perspective to read.

>Just as a test, how would Mathematica handle the following [or part
>of] little task:

>search all files in Dir=tree D |
>which are less than N days-old | and which contain string S1 | and
>which contain string S2 .

SetDirectory[dirname];
Cases[FileNames[],
(FileType[#]===File &&
StringCases[#, s1]!={} &&
StringCases[#, s2]!={} &&
(Subtract@@(AbsoluteTime /@ {Date[][[;; 3]], FileDate[#][[;; 3]]}/86400)<age)&]

No need to pipe results from one operation to the next. And the
above syntax is platform neutral.

>Try: Search in table of ListOfOpenFiles for lineS with path-P
>[field] | which have same tty-field as line with path-P2 & program-M [field]

Less clear here to me what it is you are looking for, but
FindList is probably the right function.


Unknown

unread,
May 7, 2014, 2:45:05 AM5/7/14
to
Here's a related real-live problem:
list all files in Dir-tree:D |
which are less-than daysOld:N |
and contain "egal" in the FullPathName |
and contain String:"uid" OR "UID"

Daniel Lichtblau

unread,
May 8, 2014, 4:08:17 AM5/8/14
to

A query such as this might do what you have in mind. Here
directoryname should be changed to whatever directory you
want searched.

Select[FileNames[("*uid*" | "*UID*"), directoryname, Infinity],
((StringMatchQ[DirectoryName[#] , "*egal*"] && Today <
DayPlus[FileDate[#], 3]) &]

Also there is a case sensitive switch if you want to allow strings with
e.g. "uID". If you want to allow that uid only in the name and not full
string

Daniel Lichtblau
Wolfram Research

-------------------

Daniel Lichtblau

unread,
May 9, 2014, 2:05:24 AM5/9/14
to
.

Daniel Lichtblau

unread,
May 9, 2014, 2:05:44 AM5/9/14
to

Daniel Lichtblau

unread,
May 9, 2014, 2:06:45 AM5/9/14
to

On 05/08/2014 09:20 AM, Hans Michel wrote:
> Daniel:
>
> Now What's the catch with Today?
>
> Today will only work in Wolfram Language (Mathematica v 10), which may be
> officially released Tomorrow?
>
> One can go on with a Who's on First theme. (Goes along with W|A baseball
> data)
>
> But currently the DateObject "Today" would have to be changed to something
> that would work in Mathematica 9 or below.
>
> Hans

Hans et al,

You are correct, I was using an unreleased version. Actually some good
came of it (I found a weakness in date comparisons that got addressed).
I'll leave modification for versions <=9 to others more adept at this
sort of thing. I never was much good with dates...

As for baseball,

http://www.redreporter.com/2012/2/13/2796324/hus-on-first-a-modernization

Daniel

Hans Michel

unread,
May 9, 2014, 2:07:05 AM5/9/14
to
Daniel:

Now What's the catch with Today?

Today will only work in Wolfram Language (Mathematica v 10), which may be
officially released Tomorrow?

One can go on with a Who's on First theme. (Goes along with W|A baseball
data)

But currently the DateObject "Today" would have to be changed to something
that would work in Mathematica 9 or below.

Hans

Hans Michel

unread,
May 9, 2014, 2:07:26 AM5/9/14
to
I think the dates would have to be rounded to Day then the AbsoluteTime
would be more comparable.

AbsoluteTime[DayRound[DateList[], All]] <
AbsoluteTime[DayRound[DayPlus[DateList[], 3], All]]

Issues may be file dates compared to current date time, rounding and
location (timezone) issues.

DayRound is New in version 9
Switch to Take[] goes all the back to v1. AbsoluteTime v2.

AbsoluteTime[Take[DateList[], 3]] <
AbsoluteTime[Take[DayPlus[DateList[], 3], 3]]

FileDate[] in the original suggestion by Daniel only goes back to v3.

To address the poster's original questions: I don't believe that
Mathematica has issues in filtering or using a water-fall like method. I
can't speak to if Mathematica optimizes such a process.

Hans
-----Original Message-----
From: Daniel Lichtblau [mailto:da...@wolfram.com]
Sent: Thursday, May 08, 2014 9:37 AM
To: Hans Michel; math...@smc.vnet.net
Subject: Re: Chained-functional notation examples?


On 05/08/2014 09:20 AM, Hans Michel wrote:
> Daniel:
>
> Now What's the catch with Today?
>
> Today will only work in Wolfram Language (Mathematica v 10), which may
> be officially released Tomorrow?
>
> One can go on with a Who's on First theme. (Goes along with W|A
> baseball
> data)
>
> But currently the DateObject "Today" would have to be changed to
> something that would work in Mathematica 9 or below.
>
> Hans

Hans et al,

You are correct, I was using an unreleased version. Actually some good came
of it (I found a weakness in date comparisons that got addressed).
I'll leave modification for versions <=9 to others more adept at this sort
of thing. I never was much good with dates...

As for baseball,

http://www.redreporter.com/2012/2/13/2796324/hus-on-first-a-modernization

Daniel


Bill Rowe

unread,
May 9, 2014, 2:08:46 AM5/9/14
to
On 5/7/14 at 2:44 AM, d...@gmail.com (Unknown) wrote:

>Here's a related real-live problem:
>list all files in Dir-tree:D | which are less-than daysOld:N | and
>contain "egal" in the FullPathName | and contain String:"uid" OR
>"UID"

It is not entirely clear what it is you are trying to do. There
are a couple of ways to obtain a list of files in a given directory.

First, if the directory is the current working directory, then

FileName[]

will return a list of every thing at the top level of the
directory. But in this case, only the name of things at that
level are returned not the path name.

If the directory is a subdirectory of the current working
directory you can get a list of files in that directory with

FileNames["*", dirname]

This will return a relative path name, relative to the current
working directory.

If you want the full path name then using the last form above
with dirname specified with the full path name will give you
what you want.

I will assume the desired files are in the current working
directory and you want any file with "egal" in the path name at
any sub directory level with those assumptions the problem above
can be solved as:

Cases[
Flatten@StringCases[
FileNames["*",Directory[],Infinity],___~~"egal"~~___],
_?(
FileType[#]===File &&

Subtract@@(AbsoluteTime/@{Date[][[;;3]],FileDate[#][[;;3]]}/86400)>age
&&
FindList[#,{"UID","uid"}]!={} &)]

The FileType portion filters out directories, the Subtract@@
portion computes the age of the file in days and compares that
to a specified age and the FindLIst portion looks to see if the
file contains either "UID" or "uid".

Cases checks each file for the three conditions above and only
returns a those that meet all three conditions.

Note, while this isn't a difficult thing to do in Mathematica,
if all you need to do is solve this class of problem, i.e.,
something were you need a list of files meeting a specified
criteria, Mathematica really is overkill and an expensive way to
solve this class of problem.

What Mathematica can do with files is all kinds of sophisticated
numerical analysis on the file contents, analysis that could be
very time consuming to implement in something such as Perl,
Python etc.

It makes sense to solve this class of problem within Mathematica
when you already have a license to Mathematica, are familiar
with Mathematica and solving this problem is merely a subset of
a larger problem. But it definitely does not make sense to me to
buy a license for Mathematica for just the ability to locate
files in this manner. There are far less expensive solutions for
this problem.

Another thing to consider. I've been using Mathematica since
version 1.2 and pretty much on a daily basis since version 3.0.
I consider myself quite proficient using Mathematica to solve
problems I encounter. But despite using Mathematica extensively
for quite a number of years, I do not see myself as fully
mastering Mathematica. And the level of proficiency I have
achieved was not achieved in a few days or a few weeks.
Mathematica is extremely versatile and is a very rich toolset
for solving a large variety of programs. But that versatility
comes at a price. Mathematica has what I would consider a rather
steep learning curve to climb before proficiency is achieved.


roby

unread,
May 9, 2014, 2:06:05 AM5/9/14
to
Well, I think that postfixing by use of the Mathematica Postfix operator "//"
accompanied by use of Mathematica pure Function "&" with access to the last result =
by using Mathematica Slot "#" (argument to the pure Function)
mimics *nix piping best:

the following gets all files of "c:/temp" in all sub dirs
older than 200 days
containing at least one of the strings "str1", "str2" :


"c:/temp" //
FileNames["*", #, Infinity]& //
Select[#, DateDifference[FileDate[#], DateList[]] > 200 &]& //
Select[#, StringMatchQ[#, "*str1*"] || StringMatchQ[#, "*str2*"]&] &

Regards Robert

Reply all
Reply to author
Forward
0 new messages