Permalink
Browse files

Ok, so IRB isn't designed to handle infinite lazy sequences that well…

…. Now LazySeq.inspect no longer prints the sequence. Use inspect! to force it if you know the sequence is finite.
  • Loading branch information...
1 parent c777383 commit 37beaa328e918df2bca0d189a7594908bff41500 @daveray committed Oct 15, 2011
Showing with 33 additions and 2 deletions.
  1. +19 −0 README.md
  2. +10 −1 lib/familiar.rb
  3. +4 −1 test/test_familiar.rb
View
@@ -25,6 +25,25 @@ Use `Familiar.with` if you don't feel like writing `Familiar` over and over:
end
=> 4950
+# A note on IRB Usage
+Most Clojure datastructes (maps, sets, etc) will print out in IRB the same as in the Clojure REPL. The one difference is that lazy sequences will never print out automatically. This is because IRB will always try to print the result of the last expression so something like this:
+
+
+ irb(main):001:0> x = Familiar.repeatedly Familiar.vars.rand
+
+will lock up IRB as it tries to print the infinite sequence. The Clojure REPL, on the other hand, doesn't try to print the value of a newly `def'd` var so you don't have this problem.
+
+So, to inspect the value of a *finite* lazy sequence in IRB, use the `inspect!` method:
+
+ irb(main):001:0> f = Familiar
+ => Familiar
+ irb(main):002:0> x = f.repeatedly f.vars.rand
+ => #<Java::ClojureLang::LazySeq:0x4ab3a5d1>
+ irb(main):003:0> f.take(2, x)
+ => #<Java::ClojureLang::LazySeq:0x7361b0bc>
+ irb(main):004:0> f.take(2, x).inspect!
+ => "(0.5428756368923673 0.598041516780956)"
+
# Functions
Make a function from a proc or lambda:
View
@@ -47,7 +47,8 @@ def self.method_missing(meth, *args, &block)
end
# Make inspect and to_s look right in irb
- [Java::ClojureLang::LazySeq,
+ [
+ #Java::ClojureLang::LazySeq,
Java::ClojureLang::PersistentVector,
Java::ClojureLang::PersistentList,
Java::ClojureLang::PersistentArrayMap,
@@ -72,6 +73,14 @@ def inspect
end
end
+ [ Java::ClojureLang::LazySeq ].each do |x|
+ x.class_eval do
+ def inspect!
+ Familiar.pr_str self
+ end
+ end
+ end
+
# Run a block of code without having to qualify everything in this module:
#
# Examples:
View
@@ -36,7 +36,6 @@ def test_inspect_looks_right
assert_equal '#{1 2 3 4 5}', Familiar.hash_set(1, 2, 3, 4, 5).inspect
assert_equal 'hi', Familiar.symbol("hi").inspect
assert_equal ':hi', Familiar.keyword("hi").inspect
- assert_equal '(0 1 2 3)', Familiar.range(4).inspect
assert Familiar.atom('hi').inspect =~ /^#<Atom@\h+: "hi">$/
assert Familiar.ref('hi').inspect =~ /^#<Ref@\h+: "hi">$/
assert Familiar.agent('hi').inspect =~ /^#<Agent@\h+: "hi">$/
@@ -45,6 +44,10 @@ def test_inspect_looks_right
assert_equal '(1 2 3 4)', Familiar.rest(Familiar.range(5)).inspect
end
+ def test_can_force_lazyseqs_with_inspect!
+ assert_equal '(0 1 2 3)', Familiar.range(4).inspect!
+ end
+
def test_can_create_a_function_from_a_lambda
f = Familiar.fn(lambda {|x| x * 2 })
assert f.is_a? Java::clojure.lang.IFn

0 comments on commit 37beaa3

Please sign in to comment.