From 847f872df8b2d69888a5ccc6d0a791b742d3a284 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 5 Apr 2020 11:54:10 +0200 Subject: [PATCH] README --- README.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7790b655..52e58bcc 100644 --- a/README.md +++ b/README.md @@ -714,19 +714,22 @@ This can be useful for talking to Docker: Babashka comes with the [nrepl/bencode](https://github.com/nrepl/bencode) library which allows you to read and write bencode messages to a socket. A -simple example which gets the Clojure version from an nREPL server started with -`lein repl`: +simple example which evaluates a Clojure expression on an nREPL server started +with `lein repl`: ``` clojure (ns nrepl-client (:require [bencode.core :as b])) -(let [s (java.net.Socket. "localhost" 65274) - out (.getOutputStream s) - in (java.io.PushbackInputStream. (.getInputStream s)) - _ (b/write-bencode out {"op" "describe"}) - clojure-version (get-in (b/read-bencode in) ["versions" "clojure" "version-string"])] - (String. clojure-version "UTF-8")) ;;=> "1.10.0" +(defn nrepl-eval [port expr] + (let [s (java.net.Socket. "localhost" port) + out (.getOutputStream s) + in (java.io.PushbackInputStream. (.getInputStream s)) + _ (b/write-bencode out {"op" "eval" "code" (pr-str expr)}) + value (get (b/read-bencode in) "value")] + (read-string value))) + +(nrepl-eval 65274 '(+ 1 2 3)) ;;=> 6 ```