@@ -62,15 +62,47 @@ object NonEmptyLazyList extends NonEmptyLazyListInstances {
62
62
def fromSeq [A ](as : Seq [A ]): Option [NonEmptyLazyList [A ]] =
63
63
if (as.nonEmpty) Option (create(LazyList .from(as))) else None
64
64
65
- def fromLazyListPrepend [A ](a : A , ca : LazyList [A ]): NonEmptyLazyList [A ] =
66
- create(a +: ca )
65
+ def fromLazyListPrepend [A ](a : => A , ll : => LazyList [A ]): NonEmptyLazyList [A ] =
66
+ create(a #:: ll )
67
67
68
- def fromLazyListAppend [A ](ca : LazyList [A ], a : A ): NonEmptyLazyList [A ] =
69
- create(ca :+ a )
68
+ def fromLazyListAppend [A ](ll : => LazyList [A ], a : => A ): NonEmptyLazyList [A ] =
69
+ create(ll #::: a #:: LazyList .empty )
70
70
71
71
def apply [A ](a : => A , as : A * ): NonEmptyLazyList [A ] =
72
72
create(a #:: LazyList .from(as))
73
73
74
+ // allows the creation of fully lazy `NonEmptyLazyList`s by prepending to this
75
+ def maybe [A ](ll : => LazyList [A ]): Maybe [A ] = new Maybe (() => ll)
76
+
77
+ final class Maybe [A ] private [NonEmptyLazyList ] (private [this ] var mkLL : () => LazyList [A ]) {
78
+ // because instances of this class are created explicitly, they might be
79
+ // reused, and we don't want to re-evaluate `mkLL`
80
+ private [this ] lazy val ll = {
81
+ val res = mkLL()
82
+ mkLL = null // allow GC
83
+ res
84
+ }
85
+
86
+ def #:: [B >: A ](elem : => B ): NonEmptyLazyList [B ] =
87
+ create(elem #:: ll)
88
+ def #::: [B >: A ](prefix : => NonEmptyLazyList [B ]): NonEmptyLazyList [B ] =
89
+ create(prefix.toLazyList #::: ll)
90
+ def #::: [B >: A ](prefix : => LazyList [B ]): Maybe [B ] =
91
+ new Maybe (() => prefix #::: ll)
92
+ }
93
+
94
+ final class Deferrer [A ] private [NonEmptyLazyList ] (private val nell : () => NonEmptyLazyList [A ]) extends AnyVal {
95
+ def #:: [B >: A ](elem : => B ): NonEmptyLazyList [B ] =
96
+ create(elem #:: nell().toLazyList)
97
+ def #::: [B >: A ](prefix : => NonEmptyLazyList [B ]): NonEmptyLazyList [B ] =
98
+ create(prefix.toLazyList #::: nell().toLazyList)
99
+ def #::: [B >: A ](prefix : => LazyList [B ]): NonEmptyLazyList [B ] =
100
+ create(prefix #::: nell().toLazyList)
101
+ }
102
+
103
+ implicit def toDeferrer [A ](nell : => NonEmptyLazyList [A ]): Deferrer [A ] =
104
+ new Deferrer (() => nell)
105
+
74
106
implicit def catsNonEmptyLazyListOps [A ](value : NonEmptyLazyList [A ]): NonEmptyLazyListOps [A ] =
75
107
new NonEmptyLazyListOps (value)
76
108
}
@@ -118,11 +150,7 @@ class NonEmptyLazyListOps[A](private val value: NonEmptyLazyList[A])
118
150
final def +: [AA >: A ](a : AA ): NonEmptyLazyList [AA ] =
119
151
prepend(a)
120
152
121
- /**
122
- * Alias for [[prepend ]].
123
- */
124
- // TODO: `a` should be by-name and this method should not be listed as an
125
- // alias for `prepend`, but it's too late to change that in this version
153
+ @ deprecated(" use Deferrer construction instead" )
126
154
final def #:: [AA >: A ](a : AA ): NonEmptyLazyList [AA ] =
127
155
prepend(a)
128
156
@@ -139,52 +167,76 @@ class NonEmptyLazyListOps[A](private val value: NonEmptyLazyList[A])
139
167
append(a)
140
168
141
169
/**
142
- * concatenates this with `ll`
170
+ * Concatenates this with `ll`; equivalent to `appendLazyList`
171
+ */
172
+ final def concat [AA >: A ](ll : => LazyList [AA ]): NonEmptyLazyList [AA ] =
173
+ appendLazyList(ll)
174
+
175
+ /**
176
+ * Alias for `concat`
143
177
*/
144
- final def concat [AA >: A ](ll : LazyList [AA ]): NonEmptyLazyList [AA ] =
145
- create(toLazyList ++ ll)
178
+ final def ++ [AA >: A ](ll : => LazyList [AA ]): NonEmptyLazyList [AA ] =
179
+ concat( ll)
146
180
147
181
/**
148
- * Concatenates this with `nell`
182
+ * Concatenates this with `nell`; equivalent to `appendNell`
149
183
*/
150
- final def concatNell [AA >: A ](nell : NonEmptyLazyList [AA ]): NonEmptyLazyList [AA ] =
151
- create(toLazyList ++ nell.toLazyList )
184
+ final def concatNell [AA >: A ](nell : => NonEmptyLazyList [AA ]): NonEmptyLazyList [AA ] =
185
+ appendNell( nell)
152
186
153
187
/**
154
- * Alias for concatNell
188
+ * Alias for ` concatNell`
155
189
*/
156
- final def ++ [AA >: A ](nell : NonEmptyLazyList [AA ]): NonEmptyLazyList [AA ] =
190
+ final def ++ [AA >: A ](nell : => NonEmptyLazyList [AA ]): NonEmptyLazyList [AA ] =
157
191
concatNell(nell)
158
192
159
193
/**
160
194
* Appends the given LazyList
161
195
*/
162
- final def appendLazyList [AA >: A ](nell : LazyList [AA ]): NonEmptyLazyList [AA ] =
163
- create(toLazyList ++ nell )
196
+ final def appendLazyList [AA >: A ](ll : => LazyList [AA ]): NonEmptyLazyList [AA ] =
197
+ create(toLazyList #::: ll )
164
198
165
199
/**
166
200
* Alias for `appendLazyList`
167
201
*/
168
- final def :++ [AA >: A ](c : LazyList [AA ]): NonEmptyLazyList [AA ] =
169
- appendLazyList(c)
202
+ final def :++ [AA >: A ](ll : => LazyList [AA ]): NonEmptyLazyList [AA ] =
203
+ appendLazyList(ll)
204
+
205
+ /**
206
+ * Prepends the given NonEmptyLazyList
207
+ */
208
+ final def appendNell [AA >: A ](nell : => NonEmptyLazyList [AA ]): NonEmptyLazyList [AA ] =
209
+ create(toLazyList #::: nell.toLazyList)
210
+
211
+ /**
212
+ * Alias for `appendNell`
213
+ */
214
+ final def :++ [AA >: A ](nell : => NonEmptyLazyList [AA ]): NonEmptyLazyList [AA ] =
215
+ appendNell(nell)
170
216
171
217
/**
172
218
* Prepends the given LazyList
173
219
*/
174
- final def prependLazyList [AA >: A ](c : LazyList [AA ]): NonEmptyLazyList [AA ] =
175
- create(c ++ toLazyList)
220
+ final def prependLazyList [AA >: A ](ll : => LazyList [AA ]): NonEmptyLazyList [AA ] =
221
+ create(ll #::: toLazyList)
222
+
223
+ /**
224
+ * Alias for `prependLazyList`
225
+ */
226
+ final def ++: [AA >: A ](ll : => LazyList [AA ]): NonEmptyLazyList [AA ] =
227
+ prependLazyList(ll)
176
228
177
229
/**
178
230
* Prepends the given NonEmptyLazyList
179
231
*/
180
- final def prependNell [AA >: A ](c : NonEmptyLazyList [AA ]): NonEmptyLazyList [AA ] =
181
- create(c .toLazyList ++ toLazyList)
232
+ final def prependNell [AA >: A ](nell : => NonEmptyLazyList [AA ]): NonEmptyLazyList [AA ] =
233
+ create(nell .toLazyList #::: toLazyList)
182
234
183
235
/**
184
236
* Alias for `prependNell`
185
237
*/
186
- final def ++: [AA >: A ](c : NonEmptyLazyList [AA ]): NonEmptyLazyList [AA ] =
187
- prependNell(c )
238
+ final def ++: [AA >: A ](nell : => NonEmptyLazyList [AA ]): NonEmptyLazyList [AA ] =
239
+ prependNell(nell )
188
240
189
241
/**
190
242
* Converts this NonEmptyLazyList to a `NonEmptyList`.
0 commit comments