Clojure Spec Tutorial

Поділитися
Вставка
  • Опубліковано 25 гру 2024

КОМЕНТАРІ •

  • @eugenej.5584
    @eugenej.5584 3 роки тому +3

    `conform` also has an inverse function `unform`. They are useful for converting between linear (like a list) and nested (like a map) shapes of data. You can imagine heaving a compressed data in a list that you save into a file or send via network. Types of values specified by their position in the list. When you want to work with it you conform it into a nested (map) view using a spec and when you done you "save" it back into linear view via unform. You can implement "(reform ) " - takes and returns lists but applies passed function to the collection as if it was in the nested form instead of a list. Kinda cool :D

  • @rorycawley
    @rorycawley Рік тому +2

    You might well be the superpower that the Clojure community needs, great video!

  • @joneskamikaze6228
    @joneskamikaze6228 10 місяців тому

    Thank you for sharing your experience, I am learning clojure and your videos help a lot!

  • @eugenej.5584
    @eugenej.5584 3 роки тому +5

    Cool video as always! It is important to point out that the test check thingy uses argument specs to generate test values and passes them to the function, then it checks if every return value matches the function's return spec. Another cool feature is that test check can shrink error - when it finds a mismatching value, it will try to reproduce a minimal failing case.

    • @onthecodeagain
      @onthecodeagain  3 роки тому +3

      Thanks a lot dude! Yeah I realised I may have went a bit quick over the testing part... I have a generative testing video lined up also.

  • @roberthaisfield3349
    @roberthaisfield3349 3 роки тому +4

    Hey! I just want to say that the way you actually showed a common error at 8:10, diagnosed it, and then fixed it was really helpful! A lot of educational programming content just shows the happy path, but as a beginner I mess up a lot.
    This makes me think that a video where you explain common error messages (or just tacking that on to other subject specific videos) would also be helpful!

    • @onthecodeagain
      @onthecodeagain  3 роки тому

      Thanks for the comment man! :) its super tricky to do a video based on that because different errors pop up doing different things. If you have a particular error that you want help with i can try?

    • @roberthaisfield3349
      @roberthaisfield3349 3 роки тому

      @@onthecodeagain Hmm I'll share confusing error messages as they come, but I tried this code:
      (ns emotion-modeling-lfb.core
      (:require [clojure.spec.alpha :as s]
      [clojure.spec.test.alpha :as stest]
      [clojure.spec.gen.alpha :as sgen]))
      (s/def ::between-0-100
      (s/and int?
      #(> 101 % 0)))
      (s/exercise ::between-0-100)
      And got this error message on the exercise function:
      Execution error (FileNotFoundException) at emotion-modeling-lfb.core/eval1617 (form-init13010486330272312439.clj:1).
      Could not locate clojure/test/check/generators__init.class, clojure/test/check/generators.clj or clojure/test/check/generators.cljc on classpath.
      And `[org.clojure/test.check "1.1.0"]` is in my Leiningen project dependencies

    • @roberthaisfield3349
      @roberthaisfield3349 3 роки тому +1

      Update: Just tried restarting the REPL and it worked. I was just reloading the file into the REPL, which I now know isn’t enough.

  • @Lambdaphile
    @Lambdaphile 2 роки тому +1

    Thanks for sharing! 🧙‍♂️

  • @sadface7457
    @sadface7457 3 роки тому +2

    Spectacular video.

    • @onthecodeagain
      @onthecodeagain  3 роки тому

      Thanks a lot! :D (I wish I thought of that in my video...)

  • @ovistoica
    @ovistoica 3 роки тому +1

    Thanks man! I was literally just starting using this in my clojure API.

    • @onthecodeagain
      @onthecodeagain  3 роки тому

      Thats awesome :) I hope you found this useful!

  • @andrewstebbing1646
    @andrewstebbing1646 3 роки тому +2

    Great video with nice clear explanations.
    In a real project would you place the spec along side each function, or create a new 'test' namespace with all the specs and tests?

    • @onthecodeagain
      @onthecodeagain  3 роки тому

      Awesome thanks! :) I would say it depends on how you organize your project. Im fond of separate namespaces and files though

  • @SebastianAnklamm
    @SebastianAnklamm 3 роки тому +1

    great stuff as always

  • @eugenej.5584
    @eugenej.5584 3 роки тому +1

    There is also ::foo/bar qualified key form. It is useful if you have to deal with a lot of qualified keys from different namespaces.

    • @eugenej.5584
      @eugenej.5584 3 роки тому

      foo is an alias for a namespace.

    • @onthecodeagain
      @onthecodeagain  3 роки тому

      Yeah, a video on ns (like you suggested) would be a great place to include this!

  • @edwardciafardini
    @edwardciafardini 2 роки тому +1

    on the code again
    ❤‍🔥

  • @professorclever6487
    @professorclever6487 3 роки тому +1

    Quick note on the end of the video: you could also use preconditions and postconditions to check the input and output values of a function. E.g. {:pre [(cond1) (cond2]}. They do however, throw Errors in case of failure, which may not be what you want.

    • @onthecodeagain
      @onthecodeagain  3 роки тому +1

      Think ill do a video on function meta data at some point and show that also

  • @eugenej.5584
    @eugenej.5584 3 роки тому +1

    Btw defn-spec (from orchestra) also checks return specs by default.

  • @someoneintheuniverse
    @someoneintheuniverse Рік тому

    Hello, I am using intellij instead of vs code and at 14:37 I don't see (in my ide) a way to reload the terminal (I use the terminal instead of calva, I just open the terminal and then type the clojure command), how can I make the terminal reloads? and btw, very good tutorial

  • @omarojmb
    @omarojmb 3 роки тому +1

    great video!

  • @vxanica
    @vxanica 3 роки тому +1

    please make more live coding videos like this

    • @onthecodeagain
      @onthecodeagain  3 роки тому

      posting a new vid on Monday :) I haven't been active this last month because of covid :/

  • @eugenej.5584
    @eugenej.5584 3 роки тому +2

    You can get a sample values satisfying a spec with `s/exercise`

  • @sadface7457
    @sadface7457 3 роки тому +1

    What is your view on spec as an ad hoc type system?

    • @eugenej.5584
      @eugenej.5584 3 роки тому +1

      With specs you can create a type (contract) like "map function should return collection with the same number of elements as the input argument that also a collection" or "sort function should return sorted collection" Good luck doing it with a type system...

    • @onthecodeagain
      @onthecodeagain  3 роки тому +1

      Spec is a step above types :) it allows you to implement design-by-contract in your project (en.wikipedia.org/wiki/Design_by_contract) which can garuntee that your functions and their behaviour work exactly how they should.

  • @mgetommy
    @mgetommy 2 роки тому +1

    Really good video. :)

  • @YisraelDovL
    @YisraelDovL 3 роки тому +2

    OY! Am I the only one that is being blinded by the light theme ? I enjoy the videos, but I wish there was a filter to turn down the brightness.

    • @onthecodeagain
      @onthecodeagain  3 роки тому +1

      Haha was wondering when I'd get a light theme comment. I'll use dark mode in future 👌🏽

    • @YisraelDovL
      @YisraelDovL 3 роки тому +1

      @@onthecodeagain Not specificaly "dark mode" but "darker mode" :) . BTW why not sync your videos to lbry / odysee would be happy to send you some tips there.

    • @onthecodeagain
      @onthecodeagain  3 роки тому

      @@YisraelDovL yeah please send me tips!

    • @YisraelDovL
      @YisraelDovL 3 роки тому +1

      @@onthecodeagain Can't find your lbry channel , post a link to there.

    • @onthecodeagain
      @onthecodeagain  3 роки тому +2

      @@YisraelDovL I didnt know about lbry / odysee before these comments :p Just signed up, apparently the account sync can take up to a week... when its finished I'll post a link to the channel here. Appreciate the help!

  • @dayumnson9769
    @dayumnson9769 3 роки тому +1

    great video again, keep it up!
    I didn't quite understand the generated tests part...
    Whats the use case of that function you created for the :ret?
    If it generates 1000 possible values how can we even have conditions here? They are dedtined to fail then, no?
    What would be a use case/ :ret fn in the real world? Or in anpractical example?
    Once again the question might be stupid, have not yet coded more than the firdt chapter of the guide in clojure page, but I am in love with this language.
    greetings

    • @onthecodeagain
      @onthecodeagain  3 роки тому

      Hey hows the learning going?
      A practical example of ret would be to ensure that the type that was returned is valid or if its within a certain number range or matches any constraints you can think of for what your return value should be