547 points · by tree_of_item · 11y ago · 189 comments
As is you would have to put in filters to resplit input into lines and that is very messy for something that you will need/want to do very often.
For example if you wanted to parse by character it would be wonderful to be able to do the following:
STDIN | /./{|c|
# stuff
}
Even better would be if you took it a step further and offered something like regex pattern matching for the block input. e.g. STDIN | /\w+/{|word|
/house/ {
# when word is house
}
/car/ {
# when word is car
}
{
# default case
}
}Reference: https://www.gnu.org/software/sed/manual/sed.html#Examples
Sorry, I couldn't miss that one. :) Really, check the example. Sed is most powerful tool and can do astonishing work.
It would however be nice to have a tool with similar power but simpler, more comprehensible syntax. One of the other commenters linked to an interesting document on sam, which has a better control flow but equally arcane syntax.
I'd argue that makes it one of the least useful documents you've ever read :)
A job for life! :-)
Streams are a non-trivial construction, after all.
"~/yourfile.txt" utf8 file-contents
"\\w+" findall [ first second ] map [
{
{ "house" [ "house stuff" print ] }
{ "car" [ "car" print ] }
[ "default stuff: %u\n" printf ]
} case
] each
Tacit programming basically is the same thing as stream programming.Yes, splitting and combining. Looking at the code example, I feel like so much opportunity is just being squandered, where something like Rust's mapping constructs would feel so much better. Yours is definitely more in that vein, here's something a little closer:
STDIN | /\w+/{|word|
/house/ => "foo"
/car/ => "bar"
"literal" => "baz"
_ => "dib"
# (where _ is a magical symbol for the default case... could be anything)
}Might also be nice to borrow some of Scala's features for terser code than Ruby, like
ary | { _ + 5 }
instead of ary | { |el| el + 5 } {|el| el + 5}
more than { _ + 5 }
the latter is too implicit for my taste, also I imagine it could become more confusing in a more complex context. STDIN | /\w+/ | {|each match|
/house/ => "foo"
/car/ => "bar"
"literal" => "baz"
_ => "dib"
}
Where "each x" in a parameter list wraps the body in a map and uses x as the loop variable, and "match" in a parameter list instructs that this is the parameter we are doing pattern matching on in the body (no need to name it).I think this is a more flexible solution, and more modifiers than each could be introduced. For instance, sum of squares could be:
seq(100) | {|each x| x * x} | {|reduce (x = 0, y)| x + y}It shows well how a slight shift on the problem expression can lead to a more powerful, simpler to use and general solution. In this case, the shift is to design stream processors (like `sed` and `awk`) to work on streams of text chunks defined by regular expressions rather on streams of lines matching regular expressions.
Regarding this concern, it's hard to say what is the plan of Matz for Streem with a single code example. But the fact, that this sample code processes a stream of integers, lets hope a tool with more flexibility than `sed` or `awk` to specify what is processed along the streams.
The link was posted on HN some time ago and was generally well received. I didn't have the time to use it much yet, but it looks very nice. Well, unless you hate lisp.
Some ways of doing UNIX-style pipes in Python:
http://jugad2.blogspot.in/2011/09/some-ways-of-doing-unix-st...
And that later inspired me to create an experimental tool called pipe_controller:
Swapping pipe components at runtime with pipe_controller:
http://jugad2.blogspot.in/2012/10/swapping-pipe-components-a...
The above post links (recursively) to a few others, also by me, describing a couple of other ways of using pipe_controller, including this one:
Using PipeController to run a pipe incrementally:
http://jugad2.blogspot.in/2012/09/using-pipecontroller-to-ru...
and also includes the link for it on my Bitbucket account.
There's also plumbum, another such Python module.
I'm playing with the idea of build a language, where the functions are unix-like, with STDIN, OUT & ERR. So, instead of raise a exception, it put data in ERR... and make it easy to compose them.
If you google stream processing language you will also get a whole bunch that are not text specific. They are definitely worth looking at if you are interested in building any kind of stream based language.
I shared a small story about him and languages quite a while ago, I guess it fits here as well: https://news.ycombinator.com/item?id=6562979
Also, he seems like a nice guy, not the type to be drawn into the kind of flareups in which he would draw flak.
The thing is they are hampering Ruby now because people want to use Ruby for all kinds of things he probably never intended it for.
E.g. I'm on a crazy multi-year journey towards doing an ahead of time compiler for Ruby. Ruby is not designed for that. It has dozens of issues that makes it incredibly hard to do that efficiently compared to many other languages.
At the same time, MRI was incredibly inefficient in earlier incarnations because it was simple. It interpreted the syntax tree. Easy implement. A nightmare to make fast.
Ruby's grammar is also a massive pain for people who want to implement Ruby. Great for developers mostly until you run into some of the hairy corner cases that are often a result of trying to be incredibly clever to make things flow very naturally when writing Ruby most of the time, which has led to a lot of byzantine rules.
I wouldn't give Matz flak over it, simply because some of it are part of what makes Ruby so pleasant to use, and others are simply artefacts of him meeting his needs while implementing Ruby rather than designing it to meet some ideal that wasn't necessarily very relevant to him then.
But I'm not surprised (a bit sad, but not surprised) if someone makes it personal.
I should clarify that I meant Ruby the implementation, not Ruby the language.
Edit: I changed the word to "some", maybe that was a bit of a hyperbole. I see quite some unreflected bashing though.
Sure, you may end up discarding your ideas yourself, but if you instead sought to implement them you could learn something in the way, maybe turn them into something that would actually make sense. That would not happen if you just followed advice from some authority and simply decided not to experiment with your ideas any further. So I'd not discard advice from knowledgeable people, but I would not take it as a final word either.
I'm not sure everyone is familiar enough with Ruby to know who Matz is.
EDIT: Interesting fact: I believe this is now my most downvoted comment in five years on HN, at effectively -7. Never would have guessed. I'm not exactly sure what it says, but I thought it was an interesting data point.
"Yukihiro Matsumoto, creator of Ruby"
Can anyone else comment on this? Most canonical FizzBuzz programs special case 15, and I don't get it.
for i in range(1i, 101) {
match (i % 3, i % 5) {
(0, 0) => println!("Fizzbuzz"),
(0, _) => println!("Fizz"),
(_, 0) => println!("Buzz"),
_ => println!("{}", i),
}
}Well, sure, explicitly calling out i % 15 rather than (i % 3) && (i % 5) or the equivalent has that problem.
> In my opinion, pattern matching offers the most direct solution and comes with an abundance of compiler optimizations.
Pattern matching is not available in many languages, but, sure, where its available, its a great choice. Note that this still has a distinct case for the case where both % 3 and % 5 are true, rather than just testing those cases independently and sequentially and concatenating, so I think it falls into the general class of solutions I was describing.
fizzBuzz n
| n `mod` 15 == 0 = "FizzBuzz"
| n `mod` 3 == 0 = "Fizz"
| n `mod` 5 == 0 = "Buzz"
| otherwise = show n
main = mapM_ (print . fizzBuzz) [1..100]
I agree with you about generalizing pattern matching for less simple cases. Your example brought to mind view patterns, about which Oliver O'Charles had a nice writeup recently [1]. Nifty little extension.[1] https://ocharles.org.uk/blog/posts/2014-12-02-view-patterns....
Some of us know that clients ALWAYS change their minds, specs are rarely equivalent to the end result, and code against future changes that are trivial to account for in advance.
Additionally, using %15 is not DRY. If the spec changes from saying "Fizz" on multiples of 3 to saying "Fizz" on multiples of 4, then you will have to also update 15->20. If you forget to do this, you have a bug.
The correct implementation is dependent on the problem's context, and such context is not available with the FizzBuzz problem.
It's kind of a trivial test for that sort of recognition, but anything as small as FizzBuzz is going to be rather trivial.
Really, the FizzBuzz test is a check to see if someone is bullshitting when they say they can code. Testing deeper than that is expecting too much of it.
Of course, it can be actually shorter with a goto.
For the record, I have utterly no math background (hardly passed algebra), but I also agree that checking only 3 and 5 is the better solution and is how I've always written FizzBuzz.
if x isDivisibleBy 3: print "Fizz"
if x isDivisibleBy 5: print "Buzz"
and.. how do I now print x in the neither case? I can't 'else'. I could make a long if not divisible by either if expression, but that's less easy to read than an if/else chain that starts out with 'if divisible by both, print fizzbuzz'.If fizzbuzz was: Print FizzBuzz for multiples of 15, fizz for multiples of 3, and buzz for multiples of 5, and nothing otherwise, I bet you'd see the above pseudocode far more.
To be fair, a spec with tests was reversed out of the ruby implementation[1], so things have improved a bit.
So, while I'll agree that there are issues that come from the implementation being the spec of a language in general, I would say we are well earlier than the point at which we can identify that as a problem with Streem.
That said, I'm incredibly optimistic about a new Matz language. If I was going to guess, the syntax will be much lighter and the semantics will make VM optimization much easier than in Ruby.
let (|>) x f = f x
(|>) :: Seq a -> a -> Seq a
O(1). Add an element to the right end of a sequence. Mnemonic: a triangle with the single element at the pointy end.
It seems to be in the containers package since 2005: [1][0]: http://hackage.haskell.org/package/containers-0.5.5.1/docs/D...
[1]: https://github.com/haskell/containers/commit/1e61853dbd4b9fc...
'|>' from F# is the same as 'flip $' in haskell, or (&) imported from the lens library
The following code computes the sum of squares of all positive numbers of a list:
list |> Enum.filter(&(&1 > 0)) |> Enum.map(&(&1 * &1)) |> Enum.reduce(0, &(&1 + &2))
let (|>) x f = f x
Is basically saying: x |> f is equal to f x
So in F# it's the same as Elixir, but the value x is applied to the function f (passed as the last argument). i.e. list |> List.filter (fun x -> x > 0)
|> List.map (fun x -> x * x)
|> List.reduce (fun s x -> s + x)While I am sad to see that Ruby may be superseded by a new language I'm really happy to see Matz leading the way with one of the solutions. In the Ruby community we have a expression "Matz is nice and therefore we are nice". That has set the tone for the community in ways that have never been the same in some of the others.
As someone who has had the opportunity to talk with Matz on multiple occasions and work with the Ruby community it would be great to see this as a natural evolution of Ruby and the people who love it... As I have started to move on to working with more functional languages etc. I have started to move away from doing Ruby, but if the community can continue on and evolve with a new language that would be awesome!
Only since April? Has there been a non-bugfix release of Ruby that didn't bring in more functional concepts to Ruby since, well, ever? AFAICT, bringing more support for ideas that come from the FP world has been one of the perennial drivers of progress in Ruby, not a new issue that emerged this spring.
> To me this says that Matz is coming to the conclusion that we may need a new language to get functional right.
I think that's reading a lot into it. Matz experimenting with something doesn't mean that the intent is to replace Ruby with it.
This non sequitur annoys me. Deconstructing it:
* A: "Matz is nice": Let's say we all agree this is true.
* B: "we are nice": i.e., the ruby community is nice.
* P(A -> B): (A therefore B) is a slogan, so I assume the proposition P is believed to be true. Is it?
In order for P to be true, the only option is for B to be true as long as Matz keeps being nice. Assuming Matz is still good-natured, is not hard to find counterexamples for B (every big community have some less-than-nice people). So the facts tell us that P is false.
Alternatively, if you assume that Matz is not nice, then, without mattering if "we" are nice or not, the slogan holds true (modus ponens [1]).
Anyway, my point is that the slogan is as silly as this rant :p.
1: http://en.wikipedia.org/wiki/Modus_ponens#Justification_via_...
https://github.com/matz/streem/commit/1c8189f9e1df3289801b28...
Well, that was quick.
Now it seems to be finding literal strings (so "strings" e.t.c.). That would explain the literal double quotes on either side. so without that we get: ([^\\\"]|\\.)
so zero or more repeating versions of [^\\\"]|\\.What I don't understand is why there is the explicit or \\. construct there, as this seems unnecessary. Am I missing something? also, why does it seem that strings cannot have either literal \ or literal " in them?
https://github.com/charlesmartin14/gnu_parallel
it is badly needed
http://www2.research.att.com/~astopen/dss/dss.html
http://www2.research.att.com/~astopen/publications/dss-2004....
http://pragdave.me/blog/2007/12/30/pipelines-using-fibers-in...
true{TRAIL} return keyword_false;
Well, this will be fun to debug(-> (read) (eval) (print) (loop))
using python-sh in hy this is possible:
(-> (cat "/usr/share/dict/words") (grep "-E" "^hy") (wc "-l"))
The replacement of "}" with "end" etc, is a trivial replacement, not a different type of syntax.
Lisp, Prolog, etc, would be a non-C syntax.
That said I think you probably want more than just curly braces to define C-like even then.
Sony played hard many times, e.g. closing Linux on Playstation 3 devices. I do not condone the attack, although I have no sympathy at all for such kind of companies.