Hacker News new | ask | show | jobs
by SilasX 2712 days ago
How so? In Rust, the macro is just a very thin wrapper around some output formatting boilerplate. In Haskell, it exists because you'd otherwise have to change the type of the function to add print-debug statements.
1 comments

Because it can be used to wrap any expression.

I haven't seen a debug function that returns the value again anywhere else.

It was definitely in Arc first, and probably in other lisps before that:

https://github.com/arclanguage/anarki/blob/master/arc.arc#L1...

Edit: Yes, it's in Common Lisp too, so it goes back at least to the 1980s and probably further.

http://www.lispworks.com/documentation/HyperSpec/Body/f_wr_p...

The debug printing method p in Ruby have been returning it's argument for years now. Dbg seems possibly better for the usecase though as you get the location as well.
It also reminds me of Ruby's

  .tap { |x| puts x }
For those of you who don't know Ruby, its map/filter/reduce functions chain like this:

  values.map { |x| x + 2 }.select { |x| x > 3 }
So when you want to look at an intermediate result, there's the .tap() method that runs a lambda with that intermediate result, then passes it on to the next step in the chain.

  [0, 1, 2, 3].map { |x| x + 2 }.tap { |x| puts x }.select { |x| x > 3 }
This returns [4, 5] after printing [2, 3, 4, 5]. ("puts" is Ruby's println.)
Took me a while to find it, but Rust's iterators have an `.inspect` method that gives you a read-only reference, so println debugging works fine. For more advanced tap-like stuff, use the `tap` crate (which allows you to write `array.tap(|xs| xs.sort())` for example, even though `sort` mutates in place and doesn't return the array).