Code Monkey home page Code Monkey logo

deflet's Introduction

deflet

Clojars Project

Make let-expressions REPL-friendly!

Let them have their inline-def and eat it too.

Usage

(require '[borkdude.deflet :refer [deflet]])

Write inline-defs like you would in a Rich comment form, so you can evaluate things expression by expression as you go:

(comment
  (def x 10) ;; => #'x
  (def y (inc x)) ;;=> #'y
  y ;;=> 11
)

but now without polluting the global environment in production / library code, while still having the ability to evaluate expressions in the REPL:

(deflet
  ;; evaluation still works for individual forms in a REPL-connected editor:
  (def x 10) ;;=> #'x
  (def y (inc x)) ;;=> #'y
  ;; but the whole expression is compiled into a let-expression which returns the last value:
  y) ;;=> 11

The above deflet form expands into:

(let [x 10]
  (let [y (inc x)]
    y)) ;;=> 11

I find the inline-def style particularly helpful when exploring code in the REPL, e.g. when writing tests:

(deftest deflet-test
  (deflet
    (def x 10)
    (is (= 10 x))))

Nbb

This library also contains an nbb REPL-friendly variant, called defletp which works in concert with promesa:

(require '[borkdude.deflet :refer [defletp defp]]
         '[cljs.test :refer [deftest async is]]
         '[promesa.core :as p])

(deftest defletp-test
  (async
   done
   (-> (defletp
         (defp x (p/delay 100 :result))
         (is (= :result x)))
       (p/finally done))))

The defp works like def but wraps the result with nbb.core/await to await top level promises. So when evaluating (defp x (p/delay 100 :result)) in the nbb REPL, you'll get a var x bound to 100 instead of a promise. But the defletp macro expands this into a promesa.core/let expression.

Here is a demo of how you can use deflet with nbb and playwright tests.

deflet's People

Contributors

borkdude avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.