|
| 1 | +;; Copyright (c) Rich Hickey. All rights reserved. |
| 2 | +;; The use and distribution terms for this software are covered by the |
| 3 | +;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) |
| 4 | +;; which can be found in the file epl-v10.html at the root of this distribution. |
| 5 | +;; By using this software in any fashion, you are agreeing to be bound by |
| 6 | +;; the terms of this license. |
| 7 | +;; You must not remove this notice, or any other, from this software. |
| 8 | + |
| 9 | +(ns cljs.recur-test |
| 10 | + (:require [cljs.test :refer-macros [deftest is]])) |
| 11 | + |
| 12 | +;; Setup for CLJS-2085 |
| 13 | + |
| 14 | +(defprotocol ISearch |
| 15 | + (search [this coll])) |
| 16 | + |
| 17 | +;; Passing this in the recur call here will cause a warning to be emitted |
| 18 | +(defrecord Search1 [needle] |
| 19 | + ISearch |
| 20 | + (search [this coll] |
| 21 | + (when (seq coll) |
| 22 | + (if (= needle (first coll)) |
| 23 | + needle |
| 24 | + (recur this (rest coll)))))) |
| 25 | + |
| 26 | +;; This code will be accepted as is |
| 27 | +(defrecord Search2 [needle] |
| 28 | + ISearch |
| 29 | + (search [_ coll] |
| 30 | + (when (seq coll) |
| 31 | + (if (= needle (first coll)) |
| 32 | + needle |
| 33 | + (recur (rest coll)))))) |
| 34 | + |
| 35 | +;; This code will also be accepted as is; the recur is to a loop |
| 36 | +(defrecord Search3 [needle] |
| 37 | + ISearch |
| 38 | + (search [this coll] |
| 39 | + (loop [coll coll] |
| 40 | + (when (seq coll) |
| 41 | + (if (= needle (first coll)) |
| 42 | + needle |
| 43 | + (recur (rest coll))))))) |
| 44 | + |
| 45 | +;; This code should not cause a warning to be emitted |
| 46 | +(defrecord Search4 [needle] |
| 47 | + ISearch |
| 48 | + (search [this coll] |
| 49 | + (let [search-fn (fn [coll] |
| 50 | + (when (seq coll) |
| 51 | + (if (= needle (first coll)) |
| 52 | + needle |
| 53 | + (recur (rest coll)))))] |
| 54 | + (search-fn coll)))) |
| 55 | + |
| 56 | +(deftest cljs-2085-test |
| 57 | + (is (= 1 (-> (->Search1 1) (search [:a 1 "b"])))) |
| 58 | + (is (nil? (-> (->Search1 :z) (search [:a 1 "b"])))) |
| 59 | + (is (= 1 (-> (->Search2 1) (search [:a 1 "b"])))) |
| 60 | + (is (nil? (-> (->Search2 :z) (search [:a 1 "b"])))) |
| 61 | + (is (= 1 (-> (->Search3 1) (search [:a 1 "b"])))) |
| 62 | + (is (nil? (-> (->Search3 :z) (search [:a 1 "b"])))) |
| 63 | + (is (= 1 (-> (->Search4 1) (search [:a 1 "b"])))) |
| 64 | + (is (nil? (-> (->Search4 :z) (search [:a 1 "b"]))))) |
0 commit comments