

Idiomatic Clojure with LightTable - imdhmd
https://metaphysicaldeveloper.wordpress.com/2013/11/18/idiomatic-clojure-with-lighttable/

======
ibdknox
I think this is one of my favorite features that we've added to Light Table
recently and it shows one of the benefits of being connected to a running
process of the language you're working with: you can use the tools of that
language to write code for you. There's nothing that makes this Clojure
specific - you could do these same things in any other language too.

~~~
DanielRibeiro
OP here: thanks Chris for the amazing capabilities in Light Table. It is the
piece of software I'm most excited about these days.

~~~
ibdknox
You are quite welcome! And that's what I love to hear - we're pretty excited
too :)

------
jonase
If someone is interested in how kibit is implemented I've got some slides at
[http://jonase.github.io/kibit-demo/](http://jonase.github.io/kibit-demo/).
It's quite easy and a good (I think) introduction to core.logic.

------
jcrites
Does LightTable have autocompletion for Clojure or might it in the future?

How does autocompletion work for Lisp-like languages? I'm wondering if the
verb-subject ordering makes it more difficult to build effective
autocompletion.

In languages with a syntax like C, code phrases benefiting from autocompletion
focus on a particular thing in subject-verb-object order:

    
    
      obj.method(param);
    

Consequently I can engage auto-completion by typing the following, where
<cursor> is the location of the text cursor in the editor:

    
    
      obj.<cursor>
    

I type variable name and the "dot" (or equivalent), and the IDE can make
informed suggestions about methods to show: methods defined on obj, or methods
taking obj's type as their first input.

When writing code, I usually know what object I'm working on (autocompleting
the variable name is useful but not crucial, since it's usually on preceding
lines). What I want to know, and the area in which autocompletion helps me
most, is finding out what methods are relevant to that specific object (e.g.
defined by the object).

When I'm writing quick prototype code, I use an approach that's almost like
single static assignment:

    
    
      A a = new A();
      B b = a.foo();
      C c = b.bar();
    

Using autocomplete, I can explore objects and methods while staying within the
IDE. When combined with in-IDE documentation, writing new code is extremely
effective.

How do you effectively autocomplete in a Lisp language?

    
    
      (<cursor>
    

If I type that, there's no object yet, so what autocompletions can show up? A
list of all functions?

    
    
      (<cursor> obj ...)
    

If I type this line and move my cursor back to the first parenthesis, then it
makes sense that autocompletion could understand the context, but moving the
cursor backwards is inconvenient. It's not a fluent way to write code.

I have not used autocompletion in Lisp-based languages. How do IDEs or REPLs
solve this problem? It seems like a similar problem will affect pure-style
functional programming languages like Haskell. Static typing is not the
criterion, since Python has effective REPL autocompletion.

Does the lack of a natural opportunity to present relevant autocompletion
information inhibit the development of advanced editors for these languages?

~~~
chongli
Erik Meijer has complained about this in the past. The answer is that it
require a change in thinking. Object-oriented programming encourages the
programmer to think about objects first (duh) and later ask what methods are
available to them. Functional programming is the opposite. You think of the
verbs first; the nouns themselves are interchangeable. So the simple answer is
this:

    
    
        (fo<cursor>
    

Pressing tab (or whatever invokes your auto-complete) would pop up a list of
options such as this:

    
    
        (foo
        (foo-bar
        (foo-bar-baz
    

As for exploration, Clojure has tools such as doc and find-doc to help you
search for a particular function.

~~~
seanmcdirmid
So the noun is encoded after the verb. I've seen scheme libraries like this,
and makes me wonder there really are some non trivial purely verb libraries
out there for functional languages?

------
dopamean
I've been wanting to learn Clojure and recently bought the book Clojure
Programming (not to be confused with Programming Clojure) and I'm curious to
know if others believe it is a good idea for a Clojure novice such as myself
(lisp and JVM novice too) to use kibit while learning. Any advice would be
greatly appreciated. Thanks.

~~~
wuschel
(Confession: I am a novice to clojure/scheme/JVM myself.)

I admit that I have not tried kibit yet. However, LightTable/REPL is a great
tool for this endeavour. If only clojure could have an error tracking report
that would be comparable to the wonderful implementation that can be found
with the python IDLE.. but I guess that is too much to ask?

~~~
mrgoldenbrown
Can you elaborate on what you mean by "error tracking report" in IDLE? Either
I'm missing something cool about IDLE or I am totally misunderstanding.
Thanks!

~~~
wuschel
Sorry, what I meant was the TraceBack. It helped a great deal when learning
programming and hunt those nasty bugs:

    
    
      Traceback (most recent call last):
        File "C:/Users/wuschel/Desktop/error-track.py", line 3,   in <module>
          newstrin = teststrin + 3
      TypeError: cannot concatenate 'str' and 'int' objects
    

In clojure, you get those error reports in java lingo - I don't know how fast
you can get use to that kind of error messages when you do not know Java. Now,
this one is obvious, but it just should serve as an example:

    
    
      clojure.lang.Compiler$CompilerException: 
      java.lang.RuntimeException: clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve symbol: b in this context, compiling:(null:4:1)

~~~
mrgoldenbrown
OK, this makes sense. I'm slightly disappointed though, I was hoping to learn
a secret new python feature :)

------
peatmoss
That is quite impressive. I wish I'd have known about kibit 6 months ago.
Since I'm still a Clojure novice, I guess I still stand to benefit from it.
Moving from "can bang out technically workable code" to "effectively uses the
whole range of core functions" is a long slog. Next time I'm writing Clojure
I'll definitely be running kibit over it.

------
wuschel
I find LightTable to be a great tool when it comes to get a first impression
of clojure. Installing, configuring and getting the grasp of emacs, or
configuring Sublime Text with SublimeREPL can be a lot of hassle under Windows
based OS. LightTable helped me to save a lot of time here.

------
owenjones
This is awesome, I've been excited about Lighttable since I first heard of it.
I wish it was in time to back it.

It get's better with every release, can't wait for the finished product.

------
lispm
Not to mention that Symbolics Zmacs already did this. It for example has code
conversions built-in to change code between various UI libraries or Lisp
variants.

ftp://ftp.ai.sri.com/pub/mailing-lists/slug/930331/msg00240.html

------
senand
That's awesome, thanks for sharing!

I hope that with the recent addition of plugins, LightTable will also be able
to provide code inspection à la IntelliJ IDEA, since that's the feature I miss
most.

~~~
lemming
_shameless plug_

You could try Cursive
([http://www.cursiveclojure.com](http://www.cursiveclojure.com)). It's pretty
new but it's based on IntelliJ so we get a lot of features for free. We don't
have a lot of inspections yet but they're coming soon (including kibit-in-the-
editor type inspections).

------
agumonkey
going off-road: more and more I wish for non lisps in s-exp syntax, so you can
reuse pattern matching on ast and other niceties.

~~~
cjo
thing is if you take a modern language and add s-expressions, then you may as
well add macros and at that point there's a good chance it just became a lisp

If you go here
([http://www.paulgraham.com/icad.html](http://www.paulgraham.com/icad.html))
and search for the heading 'What Made Lisp Different' it may better explain it

~~~
agumonkey
I guess Racket amongst other things makes me want a lisp system with languages
as lib-dsl sharing s-exp syntax.

~~~
chongli
S-expressions (or lack thereof) basically define a language as being a lisp or
not.

------
owenjones
Hmm every form I try and evaluate seems to be reduced to a one line function.
Is this working as intented?

Even in the example given it's reduced the function to a 1 liner, I assumed
because it was short enough that it's still legible. Is this trying to tell me
my functions are too long?

~~~
ibdknox
Clojure doesn't have a real code formatter unfortunately. I posted a slightly
better version that preserves formatting if nothing changes and tries to use
the clojure pprinter to format as best as it can.

------
vorce
I imagine this could be super powerful for Elixir as well (which has awesome
meta programming capabilities).

------
JPKab
I'm reminded of the recent "What 4Chan thinks of HN":

"Article that is actually interesting" 0 comments

This is awesome. Thanks for sharing.

~~~
sejje
It's probably super relevant that the percentage of the community who has the
ability to comment about neat Clojure code is extremely limited (1% maybe?
perhaps even less) compared to the percentage that can comment on some techno-
political drivel.

