Clojure Journey IV – Leiningen and the famous REPL

In the previous chapters we set up our development environment, now it’s time to to get our hands dirty.

First of all we need to have Java installed, Clojure is a hosted language, and it compiles to JVM bytecode, so, we need Java installed.

After it install Leiningen fallowing the install instructions, it is the most popular build/dependency management tool for Clojure, and we’ll use it a lot while learning Clojure.

After install, just call lein at your terminal, and it should prompt help instructions.

REPL

Its is one of the most important thing in your Clojure development flow (and should be in every language, if its available), it will give you a interactive programming experience.

REPL stands for “Read–eval–print loop” its basically a interactive interpreter for one programming language, it was originally invented by LISP (the Clojure grandfather), and sice it a lot of programming languages has adopted it own (Clojure, Ruby, Python, JavaScript and much more).

This can sound like a bullshit for everyone that’s already used to programming in one language that already have one, but from the folks coming from environments that doesn’t have it, will be beginning of a new era, an era with REPL!

Just open your terminal and type lein repl and it will start a new REPL for you:

If you don’t want to use lein to start our REPL, just type clj and it will start too.

$ > lein repl
nREPL server started on port 46213 on host 127.0.0.1 - 
nrepl://127.0.0.1:46213
REPL-y 0.4.4, nREPL 0.7.0
Clojure 1.10.1
OpenJDK 64-Bit Server VM 1.8.0_252-b09
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

user=> 

Now every expression that you type will be evaluated:

user=> (+ 41 1)
42

In one future post, we’ll learn how to integrate it with our text editor and have a full interactive programming experience!

REPL is not only evaluate expressions, in the future we’ll learn how to use it for debuggind, run test and much more, but for now, we’ll use it only for evaluating expressions while learning the beginners things!

Leiningen

Visualizar(abrir em uma nova aba)

One of the most fantastic thing about leiningen is to create a new project, is just one command you have a full project skeleton:

$> lein new app hello-clojure
Generating a project called hello-clojure based on 
the 'app' template.

Now just open it inside your editor and start exploring our project! One of the things that you need to note is the file project.clj, it is used by leiningen and it have important information to it build your project, like dependencies and the main function (the first function to run in our project). I think that if you already program other languages you know what this file is. For now you just need to think a lot about this file, in the future we’ll go deep.

Just to test our project, open the file src/hello_clojure/core.clj and change the println expression to any message:

(defn -main
  "Hello Clojure, I love U!"
  [& args]
  (println "Hello, World! I'm lovin Clojure"))

Now just go to your terminal and run your project with lein run:

$ > lein run
Hello, World! I'm lovin Clojure

That’s wonderful, right? With lein we can run our tests too, just type lein test, and you will run all tests of your project (leiningen app template comes with one test that’s easy to fix, it’s your mission try it).

We created our first project hello-clojure, tested with lein run, it’s time to build it! To build is very simple, just type lein uberjar and wait it finish.

After it, you should have a jar file inside target/uberjar directory in your project, just navigate to ir, and run it as a jar:

$ > java -jar hello-clojure-0.1.0-SNAPSHOT-standalone.jar 
Hello, World! I'm lovin Clojure

Leiningen templates

Leiningen has a lot of templates to use with the command leiningen new, it has a template for almost everything, and you can check for templates here. If you already waste some time in the internet looking for examples of skeletons of projects in X languages, this is for you! if you need a skeleton for CLI, just look at it, if you need something to webapps, just look at it! Its useful a lot.

We used the app template because its the most basic template, and it represents a common structure for Clojure apps, it can be very useful on the next steps of our journey.

Conclusions

We learned a lot about Leiningen in this post, now we know how to create projects, run, test and build it… But we learned something more important, something that we’ll use a lot through our journey, we learned how to use REPL!

REPL is one of the most powerful tool that we’ll use, and it will help a lot while studying to quick evaluate expressions, get used to it, and you’ll be a great Clojure wizard.

Final thought

If you have any questions that I can help you with, please ask! Send an email (otaviopvaladares at gmail.com), pm me on my Twitter or comment on this post!

Follow my blog to get notified every new post:

Check my last posts:

Clojure Journey III – Emacs for Beginners Course

On my last post I talked about why I chose Spacemacs to be my text editor while coding Clojure, installed it and made a nice first setup. In this post I’ll talk about some workflows/commands that I think is important to have a great and smooth coding.

This post probably will be one of these posts that keep opened in your browser and you read it everytime that you need to remember something.

Neotree

First let’s talk about our tree explorer, on the last post I installed the neotree that is similar to NERDTree(from vim), and started to using it.

The first thing that I did was to bind the open/close of it to F8 key with the fallowing code:

(global-set-key [f8] 'neotree-toggle)

Now just press F8 and your neotree will open.

Opening neotree

To navigate inside Neotree just use your moving keys (arrow keys or HJKL), if you have any question about it, you can just type ? and a nice window will be displayed with everything that you may need:

Neotree help page

And this image saved me from writing at least 500 words, this image has everything that you will need related to Neotree.

If you don’t want to open neotree with F8 you can open by pressing SPC + f + t.

Window

Split windows is a common move to every programmer, to split is easy, just press ESC to go back to visual mode (if not already), and press: SPC + w + / to split vertically or SPC + w + - to split horizontally, where SPC is your space key. If you don’t want to use SPC, M-m + number will work too.

M-<letter> stand for alt + m

Spliting Emacs Window

To navigate between window is simple, just press SPC and the number of the window that you want to go, like SPC + 2 to go to window 2. But how to know the window number?

Spacemacs window numberr

If you’re using spacemacs, your window number will be displayed by default on the bottom left corner of your window, as the pink arrows of the image is pointing. The current window will always be in different color (as the example, window 2).

Opening/Searching Files

Open using neotree

If you want to open a file inside a window, the easiest way is to open using Neotree, just go to the window that you want to have a file opened, open your neotree, navigate to the file and open it.

Opening file by Neotree
Project search aka Ctrl + P

But if you don’t want to search for the file in your neotree, you want something more like the famous ctrl + p command from other text editors? In spacemacs with helm (the setup that I’m using), it’s very simple, just type: SPC + p + f and a window will open, just write the name that you’re searching for, and press ENTER to open in the current window.

Searching for a file using spacemacs and helm projectile

Buffers

Now we know how to manage our windows, is time to understand and manage buffers! Technically… Windows displays emacs buffers!

Everything that you open inside emacs resides on what is called “buffer”, every code, really everything! And the plot is, if you close your file/window with the traditional :q! (if you use vim mode), this code isn’t close at all, the buffer still alive!

Too see your buffers just type C-c + b and a window will open with a list of all buffers.

C-c stands for ctrl + c.

List buffers

But this window is not very useful…If we want to open a buffer the best way is to use the buffer search (similar to the file search that we see previously), just press C-x + b and you’ll have a full interactive search for buffers:

Helm buffers find

Just search for the wanted buffer and press enter to open it. If you use C-c + o it will open in a splitted window. (If you just want to open in the current window, just type enter)

Search on spacemacs bufferr

A nice shortcut is to press SPC + tab to change to the last buffer!

All the buffers keep alive, ok! But how do we kill them? Just press C-x + k select the buffer and press enter!

Search on project

To search for a pattern across all the project is very easy, just type: SPC + / and start writing your pattern:

Global search spacemacs

If you press C-c + o like in other commands, it will open in a splitted window.

Replace

It has a nice built-in way to do search/replace inside project too, it ‘s simple, SPC + p + R (pay attention on capitalized R) . After this it will ask for a pattern to replace, and what to replace, after this it will open a window with the first match, press y if you want to replace and n if no. Do this for all matches.

Search and replace

Search for commands

Emacs comes with a global commands search, just type M-x` and write what you’re searching for.

OFF: Themes

If you want to install more themes is very easy! You can install a layer names themes-megapack (If you don’t know how to install layers read my last post) and you’ll have more than 100 themes to choose.

OFF: Games inside editor

Emacs comes with some games installed, just open the global search with M-x and write the name of one of the games, like, tetris!

Tetris on emacs

Remember: With great power comes great responsibility.

Conclusions

This is a short and beginner-friendly emacs tutorial with all flows that I think is important to get started in a new text editor.

I hope I haven’t forgotten anything important to get started with it.

On the next posts related to Clojure, I’ll be using my spacemacs as my text editor, I’m loving to use it and I want that everyone that trries to start using it, have a great experience too.

Final thought

If you have any questions that I can help you with, please ask! Send an email (otaviopvaladares at gmail.com), pm me on my Twitter or comment on this post!

Follow my blog to get notified every new post:

Check my last posts:

Clojure Journey II – Chose a editor

My second step through my journey to learn Clojure is to setup my text editor or IDE. I’m not a evangelist of any text editor or IDE, so I started to search for what people usually use or is best to use while coding Clojure. After few minutes and several posts around the internet, I noticed the four most indicated tools:

I started to try each one.. First I removed Cursive + IntelliJ from my options because cursive is a paid plugin, and I didn’t tried it at all. Then I tried to install Nightcode, it looks amazing, but I don’t like to use IDEs, is my personal option and in a few cases I chose to use IDEs (Java and Go).

Update: @tanrax made a great comment in this post saying that Cursive plugin is free for open source contributors and students, if you’re one, check it!

Now I have two rivals text editors and I need to chose one. I started to take a look at fireplace plugin to vim, because I use my neovim on 80% of my hours of coding, but after installed it, I faced problems when trying to fix them.

Finally I decided to take a look at Emacs (Yes, it was my last option), I never tried it before because it looks scary at the first time. I’m learning a new language, why don’t learn a new editor? I give a chance to emacs and…. It looks amazing!

In this post I’ll describe my setup and how to use emacs to development with Clojure.

Emacs or Spacemacs?

If you already use emacs and have your custom setup you can skip this part aand use your setup.

But what about those who don’t use emacs?

Emacs or Spacemacs is a long discussion, and I don’t want to get on one side now. Simplifying, spacemacs is a “comunity-driven emacs” and it comes with a lot of configuration already made. If you chose to proceed with emacs you’ll spend a time searching for plugins and custom setups that are already done or easy done with space.

As I am a beginner user with emacs, I decided to proceed with spacemacs.

Installing Emacs

Before install spacemacs, first we need emacs installed.

To install emacs is very simple and they have a nice webpage with all the links needed. As a GNU/Linux user I just downloaded the tarball file of version 27.1, extracted it and compiled/installed it from the source using the traditional ./configure, make and make install. After this emacs is installed!

$ emacs --version
GNU Emacs 27.1
Copyright (C) 2020 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file 
named COPYING.

If you have any trouble, you can see the INSTALL file that come with.

Installing spacemacs

Spacemacs have a beautiful website that need to be checked, but it has a nice README on github too.

As the date of this post, install spacemacs is very easy, just clone it in the correct directory:

git clone https://github.com/syl20bnr/spacemacs ~/.emacs.d

After this if you start your emacs, it should open spacemacs, and it will start to self setup. It will ask you some questions, but I chose the default one for every question, and selected the vim mode, to use vim bindings inside emacs. It helped me a lot, because I’m used to use vim.

Custom setup

Now you have a file named .spacemacs on your user directory, and this file is your configuration file, similar to other text editors.

The first thing that I noticed when I started it for the first time was the size of the font, it looks very small on my monitor. To change this is easy, open your .spacemacs file (Using your spacemacs, right?), and change your size:

dotspacemacs-default-font '("Source Code Pro"
                               :size 19
                               :weight normal
                               :width normal
                               :powerline-scale 1.1)

After this next time that you open spacemacs, your text size will be better.

Other thing that I missed from vim/nvim was the famous NERDTree plugin that is a file system explorer. Fortunately I found a layer named Neotree that is basically the same thing.

To install it is simple, as the documentation says, just add the layer name neotree to the dotspacemacs-configuration-layers list.

dotspacemacs-configuration-layers
   '(
     helm
     emacs-lisp
     neotree
     )

And if you want to open the tree pressing one ke, you can paste the fallowing code inside your config file:

(global-set-key [f8] 'neotree-toggle)

And if you like to have a system explorer looking like the vim’s one, just put this in your config file:

(setq neo-theme 'nerd)

And not, just reload your emacs:

The the only thing missing in our editor now is the Clojure support, and to install it is simple, just go to the same place where you installed the neotree layer, and write “clojure”, re-open your editor, and it’s done! The Clojure support for emacs is amazing and we’lll see in the next post!

What is spacemacs layers?

The first thing to clarify is that layers is a concept from spacemacs and not from emacs.

A layer can be defined as a collection of packages and it’s configuration required to make them work well with spacemacs and each other. You can easily find a open-source layer for almost everything that you’ll need (almost), but of course, you can make private layers too.

To install a layer is easy, just follow the same steps of this post: Add its name to dotspacemacs-configuration-layers list. You can check it’s documentation to learn how to use them.

Conclusions

In this post I chose my text editor that I’ll use when coding Clojure. On the next post we’ll talk about CIDER, The Clojure Interactive Development Environment for Emacs! And spoiler: It’s amazing.

Final thought

If you have any questions that I can help you with, please ask! Send an email (otaviopvaladares@gmail.com), pm me on my Twitter or comment on this post!

Clojure Journey I – First Steps

This week I started my journey through learning Clojure. Sometime ago I’ve a brief view of LISP when I read the land of lisp book, I was very interesting, for me it’s a language with a simple concept, that on the first view looks ugly and hard, but when you understand more it’ts fabulous and genius.

Clojure is a LISP dialect hosted on JVM created by Rich Hickey It first version was published in 2005 and today it’s not one of the most popular languages, but it have a nice and great community around, working on tools, library and documentation.

If you want to learn more about Clojure and its design decisions, Rich Hickey wrote a nice (and long) paper, about its features, design decisions and history, just click here.

My first steps was to chose a book to start learning, after a few search on google, I decided to follow the famous book Clojure for the Brave and True it looks a nice book and it’s free, if I like it I can buy a physical copy to reward the author.

As my second step I like to get involved with the community, I started to follow the Clojure subreddit /r/Clojure, and the newsletter listed here.

As my next step I’ll setup my code editor, but this is the subject of the next post.

If you have any questions that I can help you with, please ask! Send an email (otaviopvaladares@gmail.com), pm me on my Twitter or comment on this post!

Follow my blog to get notified every new post:

Newsletter 5 – 08/2020

Featured

OpenAI’s GPT-3 may be the biggest thing since bitcoin – GPT-3 is the most trading topic on CS posts right now, and if you don’t what I’m talking about, just read this post, you’ll receive a nice plot!! If you feel so scared after reading it, read this post to get comfortable (In all cases I recommend both readings)

Code Only Says What it Does – Most software engineers hate documentation, we start hating it when we learn a lot of boring stuff like UML at college. But documentation is important to the team’s success in a short and long time. This post brings a nice view of the question “When documentation is important? What I need to write about?”.

Design Docs at Google – Another post about system design docs, using as example what google uses. I particularly love these docs and I think that they are very useful.

When your coworker does great work, tell their manager – This post talks about how can be important to tell managers about a great job of coworkers. Managers don’t always see the work of their reports, if you start doing it, you can help in a lot of ways.

Data Structures & Algorithms I Actually Used Working at Tech Companies – If you have questions about how companies actually use famous data structures and algorithms, this post is for you, nice examples with companies that everybody has used its products.

Chaos Engineering – A nice introduction to chaos engineering, if you never heard about that I pretty recommend that you read it.

Race Conditions/Concurrency Defects in Databases: A Catalogue – Five race conditions/concurrency defects that you can face when dealing with databases, this posts explains with nice examples and draws.

Building Domain Driven Microservices – This post is awesome! I already faced a problem of decoupling a monolith into micro-services in the past and I know how its hard and how this kind of posts help, how some concepts of DDD can help you with this task (And building new micro-services too). It brings a nice view of some points of DDD and how you can use it to help you build good micro-services.

Misc

The State of Ruby 3 Typing – Ruby 3 is coming and it will bring a type signature language and signatures for the stdlib . This post talks a little about it.

Introducing Amazon Honeycode – Not too late, AWS launches its own Microsoft Access app builder.

Go is Boring…And That’s Fantastic! – The author brings us a nice point of view about golang, it’s a biased post? Probably a little bit, but if you want to know more about the langue or why people chose it, it’s worth reading.

How Did Vim Become So Popular – I always try to post a history-like post in every edition of this newsletter, this is the one from this! This post is short and tells us background about the history of text editors, and how vim becomes so popular!

Best Practices for Background Jobs in Elixir and Handling failures in background workers with Elixir and supervisors – One of the fascinating things of Elixir is how easy is to schedule a background job and the way that supervisors can deal with our Genservers processes. This is a double-indication of posts, the two posts give a great overview of all that you need to know about it.

Prefetching? At This Age? – If you are interested in caching, CDNs, this post looks great for you, it talks a lot about cache headers and how they work with the interaction between your browser and the server.

Overview of Elixir – If you’re planning to study Elixir, this list can be a nice dictionary for you to get started with the terms used across the community.

A Terrible, Horrible, No-Good, Very Bad Day at Slack – I love to read posts describing incidents on famous companies and how they solve it. This post talks about Slack’s last big out-stage on May 12.

Choosing a Rust web framework, 2020 edition – If you’re planning to start a web project with Rust, this post can help you chose the right framework for your needs. It’s short and brings interesting key points when analyzing each one.

Exciting Days for ARM Processors – One of the biggest announcements of the year is that Apple will be switching from Intel processors to what they call “Apple Silicon” on their computers. These processors are ARM-based CPUs, but this isn’t the only good news for ARM fans, this post talks a little about this move from Apple and other two big news.

Memory Bandwidth Napkin Math – Do you know the performance of your RAM? And L1, L2, and L3 cache? This posts dives deep into benchmark memory performance and can sound pretty interesting for folks who like low-level architecture.

You’ve only added two lines – why did that take two days! – Never let a manager measure your productivity by lines of code. This is an old agreement on the programming community but needs to be remembered sometimes, this post brings it again.

[VIDEO] Brian Kernighan: UNIX, C, AWK, AMPL, and Go Programming | AI Podcast #109 with Lex Fridman – A nice video/podcast talking with Brian Kernighan, you know who he is.