Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs and tests for as-> macro #1141

Merged
merged 5 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions docs/language/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,79 @@ other case, the first false value will be returned. Example usage:
False


as->
----

.. versionadded:: 0.12.0

Expands to sequence of assignments to the provided name, starting with head.
The previous result is thus available in the subsequent form. Returns the final
result, and leaves the name bound to it in the local scope. This behaves much
like the other threading macros, but requires you to specify the threading
point per form via the name instead of always the first or last argument.

.. code-block:: clj

;; example how -> and as-> relate

=> (as-> 0 it
... (inc it)
... (inc it))
2

=> (-> 0 inc inc)
2

;; create data for our cuttlefish database

=> (setv data [{:name "hooded cuttlefish"
... :classification {:subgenus "Acanthosepion"
... :species "Sepia prashadi"}
... :discovered {:year 1936
... :name "Ronald Winckworth"}}
... {:name "slender cuttlefish"
... :classification {:subgenus "Doratosepion"
... :species "Sepia braggi"}
... :discovered {:year 1907
... :name "Sir Joseph Cooke Verco"}}])

;; retrieve name of first entry
=> (as-> (first data) it
... (:name it))
'hooded cuttlefish'

;; retrieve species of first entry
=> (as-> (first data) it
... (:classification it)
... (:species it))
'Sepia prashadi'

;; find out who discovered slender cuttlefish
=> (as-> (filter (fn [entry] (= (:name entry)
... "slender cuttlefish")) data) it
... (first it)
... (:discovered it)
... (:name it))
'Sir Joseph Cooke Verco'

;; more convoluted example to load web page and retrieve data from it
=> (import [urllib.request [urlopen]])
=> (as-> (urlopen "http://docs.hylang.org/en/stable/") it
... (.read it)
... (.decode it "utf-8")
... (drop (.index it "Welcome") it)
... (take 30 it)
... (list it)
... (.join "" it))
'Welcome to Hy’s documentation!

.. note::

In these examples, the REPL will report a tuple (e.g. `('Sepia prashadi',
'Sepia prashadi')`) as the result, but only a single value is actually
returned.


assert
------

Expand Down
2 changes: 1 addition & 1 deletion hy/core/macros.hy
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
The previous result is thus available in the subsequent form. Returns the
final result, and leaves the name bound to it in the local scope. This behaves
much like the other threading macros, but requires you to specify the threading
point per form via the name instead of always the first or last arument."
point per form via the name instead of always the first or last argument."
`(do (setv
~name ~head
~@(interleave (repeat name) rest))
Expand Down
23 changes: 23 additions & 0 deletions tests/native_tests/language.hy
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,29 @@
["X" "B" "C" "D"])))


(defn test-as-threading []
"NATIVE: test as threading macro"
(setv data [{:name "hooded cuttlefish"
:classification {:subgenus "Acanthosepion"
:species "Sepia prashadi"}
:discovered {:year 1936
:name "Ronald Winckworth"}}
{:name "slender cuttlefish"
:classification {:subgenus "Doratosepion"
:species "Sepia braggi"}
:discovered {:year 1907
:name "Sir Joseph Cooke Verco"}}])
(assert (= (as-> (first data) x
(:name x))
"hooded cuttlefish"))
(assert (= (as-> (filter (fn [entry] (= (:name entry)
"slender cuttlefish")) data) x
(first x)
(:discovered x)
(:name x))
"Sir Joseph Cooke Verco")))


(defn test-assoc []
"NATIVE: test assoc"
(setv vals {"one" "two"})
Expand Down