clojure-miami 2015-12-10

i’ll do my best to answer questions it# /cc @christianromney @adrianm

(although i don’t know cljs

thanks @bryce much of your clojure knowledge will apply anyway

@raywillig has joined the channel

a reading from the Gospel of Hickey 😛

he meant compiles down to javascript

don’t worry if you don’t know what all this means right now

you will eventually

or you do, but not in the beginner docs

the mandatory nesting is great

been schooling fools about sql order-of-operations for a week now

(in SQL, AND and OR aren’t equivalent)

not having to memorize precedence rules is nice

* has precedence over + in js, ruby

2 + 3 * 4 is 14 not 20

just use parens anyways, make your code reviewers not hate you for making ‘em look up precedence rules

hehe true

(defn my_band "cool doc string" [band] (println "my favorite band is" band))

(defn FUNCTION_NAME DOC_STRING ARRAY_OF_ARG_NAMES FUNCTION_BODY)

guessing that’s a macro around some kind of cleverness to quote stuff appropriately and transform the arg names into a let-statement

ah, vector, not array

cljs.user=> (type [])
cljs.core/PersistentVector
cljs.user=> (type "asdf")
#object[String "function String() {
    [native code]
}”]

btw gang, don’t stress if you’re not yet feeling it…there’s a lab coming up to try things in your shiny new REPLs

hang in there!

@lostlucidity has joined the channel

On the previous slide how does the function know what "band" is if "my_band" is the argument?

neat, if you put your codes in a, say, band.cljs file you can run ‘em with planck band.cljs

@lostlucidity: good question, let me attempt to recreate here

((fn [band] (println “My favorite band is “ band) “Rodrigo y Gabriela”)

let’s break this down

(fn [band] (println “My favorite band is” band)

this part creates an anonymous (or un-named) function

nevermind the JS equiv

this just makes a function

but it doesn’t call or invoke it

but if you somehow managed to call this function (we’ll cover that in a sec)

you would pass it one argument

you know it accepts a single arg by looking at the argument list enclosed in square brackets

in this case [band]

this is just the name we give the argument we are passed

so we can use it later inside the print statement

(println “Hello” band)

the band in the println

is the same band we’re given above

to call a function we place it inside parentheses

(FUNCTION-TO-CALL argument1 argument2 … argument3)

i think the slide in question might have had a typo

quick poll: plz feel free to be candid….

too fast?

just right?

too slow?

i think we’re going kind of fast, but it could just be my biases 😄

" java.lang.ArrayIndexOutOfBoundsException: 5" this returned on def duos. That okay?

lemme see

Thank you, Christian?

no sweat!

(def duos {:rodrigo :gabriela :ren :stimpy})

try that

equivalent ruby:

duos = {:rodrigo => :gabriela, :ren => :stimpy}

(def fruit  {:red “apple” :blue “blueberry”})
(fruit :red)
=> “apple”

if the commas help you, feel free to add them

yay!

(def duos {:rodrigo :gabriela, :ren :stimpy}) ;; with commas

java.lang.Exception: Unable to resolve symbol: def in this context (NO_SOURCE_FILE:0)

java.lang.Exception: Unable to resolve symbol: def in this context (NO_SOURCE_FILE:0)

make sure to run def as a function (def …)

lists are special

you MUST type single quote first

when you are using a list to hold data

like this

’(1 2 3)

why?

(:fee :fi :fo :fum) java.lang.IllegalArgumentException: Wrong number of args passed to keyword: :fee (NO_SOURCE_FILE:0)

without the single quote it’s gonna call the 1 function with arguments 2 and 3

yessir

Cool, thanks.

remember

in Clojure (or any lisp)

when you see open parens

it’s a function call

(FUNCTION-TO-CALL arg1 arg2 …)

the single quote

tells Clojure

dude, don’t call this like a function

treat it as DATA

incidentally

is just a shortcut for typing

(quote (1 2 3))

try that in your repl!

cljs.user=> '(+ 1 2 3)
(+ 1 2 3)
cljs.user=> ~'(+ 1 2 3)
undefined is not an object (evaluating 'cljs.core.unquote.call')

cljs.user=> `'(+ 1 2 3)
(quote (+ 1 2 3))

backtick

is syntax-quote

So vectors are treated as functions?

(we won’t cover that yet)

nope vectors are like arrays

[1 2 3] ;; vector

(1 2 3) ;; list

the difference between vectors and lists (why you want one instead of the other) will be covered later

i’ll make sure we spend a few minutes talking about that

Okay, so the tick is needed because paranthesis?

@bryce unquote will be (within the context of syntax-quote) ~

For lists.

correct

to tell clojure “this is not intended to be a function call"

@bryce for unquote you could do this (this example is totally contrived btw)

we’ll cover unquote, backtick and macros in general in part 4 of 4

😄

you’re way ahead

hehe

so to review

we have 4 main collection types

lists, vectors, maps, sets

'(1 2 3) ;; list

[1 2 3] ;; vector

{:fn “Mickey” :ln “Mouse”} ;; maps

#{0 1 2 3 5 8 13} ;; sets

module in Ruby

Anyone have any questions???

this part is kinda advanced

destructuring

the code on the slide is a bit hard to see

here’s a similar example

the :as key signals the rest of the map?

did we cover let already?

ok good

:as lets you give a name to the entire map

so here come some examples

(let [x 1 y 2] (+ x y) ;; => 2)

an un-found destructure turns into nil

(let [nums [1 2] [a b] nums] (+ a b)) ;; => 3

(def me {:fn “Chris” :ln “Romney”})

(let [{:keys [fn ln] :as user} me] user) ;;=> {:fn “Chris” :ln “Romney”}

(let [{:keys [fn ln] :as user} me] fn) ;;=> “Chris”

sorry i went too fast

examples corrected

Why do :as when the map has a name already?

good question

it may not always already have a name

depending on where/how you use it

so here’s one example

Got it!.. probably

Thanks.

this is a lot of material being covered very quickly

feel free to ask in this channel

anytime

I’m always here

one thing I want to tell everyone: don’t be discouraged if some of this takes a while to really sink in

it’s A LOT of material

being covered very quickly

cond vs. case: is the only difference the syntax for the default option?

case does a literal match against a value

ahhh, and cond checks for truthiness?

correct

there’s also condp

which is somewhere in between 😄

get romantic with your clojure~

could you explain that? case vs cond

jorda0mega: let me write some code

(cond
test expr
test expr)

cond evaluates each test and if it’s true, evaluates the expression

(case some-value
comparison expr
comparison expr)

case requires that you pass in some value, it evaluates comparisons, and if that comparison equals some-value, evaluates the expression

(condp some-function some-value
test expr
test expr)

condp is halfway between ‘em

it evaluates some-value, takes that value down each test, evaluates (some-function some-value test)

if that comes true, it evaluates the expression

(defn band_evaluator
  "fart"
  [band_name]
  (case (get band_quality (-> band_name
                              .toLowerCase
                              keyword))
    :great (println "you like" band_name "and have taste")
    :okay (println "i agree," band_name "is okay!")
    (println ":middle_finger::skin-tone-6::santa::skin-tone-6:your opinion of" band_name "is WRONG :middle_finger::skin-tone-6::santa::skin-tone-6:")
    ))

(defn cond_evaluator
  "fart"
  [band_name]
  (let [quality (get
                  band_quality
                  (-> band_name
                      .toLowerCase
                      keyword))]
  (cond
    (= quality :great)
    (println "you like" band_name "and have taste")
    (= quality :okay)
    (println "i agree," band_name "is okay!")
    :else
    (println ":middle_finger::skin-tone-6::santa::skin-tone-6:your opinion of" band_name "is WRONG :middle_finger::skin-tone-6::santa::skin-tone-6:")
    )))

(defn condp_evaluator
  "fart"
  [band_name]
  (condp = (get band_quality (-> band_name
                              .toLowerCase
                              keyword))
    :great (println "you like" band_name "and have taste")
    :okay (println "i agree," band_name "is okay!")
    (println ":middle_finger::skin-tone-6::santa::skin-tone-6:your opinion of" band_name "is WRONG :middle_finger::skin-tone-6::santa::skin-tone-6:")
    ))

this is how cond, case, and condp differ

and you can see my middle finger santas get really mangled too

Great intro last night! Here are some more resources:

Hey guys! Just wanted to thank everyone for coming out. You were a great audience. I’m going to be sharing my slides with the group shortly. @julian Thank you for sharing those resources. I’ll be adding to that list as well.

Om Next will be easier to digest from this https://awkay.github.io/om-tutorial/#!/