├── .gitignore
├── LICENSE.txt
├── README.md
├── pom.xml
└── src
└── main
└── scala
└── com
└── ckkloverdos
└── pipes
├── collection
├── generic
│ ├── PMap.scala
│ ├── PSeq.scala
│ └── PSet.scala
├── immutable
│ ├── PList.scala
│ ├── PMap.scala
│ ├── POption.scala
│ ├── PSeq.scala
│ ├── PSet.scala
│ └── PStream.scala
└── mutable
│ ├── JList.scala
│ ├── PSeq.scala
│ └── PSet.scala
├── concurrent
├── PFuture.scala
└── PPromise.scala
└── package.scala
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | scalapipes.iml
3 | target
4 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 |
179 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### What
2 | Scala with forward pipe operator, as in F# and OCaml.
3 |
4 | The original use-case is how to handle collections, that is why the parent package of all the code is `com.ckkloverdos.collection.pipes`.
5 |
6 | To get an idea of what you can do:
7 |
8 | import com.ckkloverdos.pipes.collection.pipes._
9 |
10 | val letters = Seq('a', 'b', 'c', 'd', 'a')
11 | val As = letters |>
12 | PSeq.filter(ch => ch == 'a')
13 | // _ works too:
14 | val As_ = letters |>
15 | PSeq.filter(_ == 'a')
16 |
17 | val numbers = IndexedSeq(1, 2, 3, 4, 5, 6)
18 | val lessThanFour = numbers |>
19 | PSeq.filter(_ < 4)
20 |
21 | val keys = Map(1 -> "keyA", 2 -> "keyB", 3 -> "keyC")
22 | val keysPlus = keys |>
23 | PMap.map { case (k, v) => (k + 1, v) } |>
24 | PMap.ofIterable
25 |
26 | Note that due to Scala's syntax quirkiness, `|>` has to be at the end of the line, so that this won't work the way you would write it in `F#`:
27 |
28 | // !! Does not compile
29 | val lessThanFour = numbers
30 | |> PSeq.filter(_ < 4)
31 |
32 |
33 |
34 | ### Why
35 | First, I did it for fun, while learning `F#` and it was a small design challenge to translate it into Scala. The pipe operator is mainstream now. It *has* been since Unix time. `Ocaml` will have it builtin in its next version (I did not check, this could be live now).
36 |
37 | I discovered the code to be more readable this way than the traditional `OO` dot chaining. Why? Because the flow is still natural (you start from the original data and you keep transforming them) and the intent is crystal clear: at each step you can actually understand both the flow of data and the flow of their respective types. I have not seen this noted elsewhere. This observation struck me as lightning and I think is of paramount importance for code readability.
38 |
39 | Currently, I am using this at a couple of projects, trying to see how it scales.
40 |
41 | ### Details
42 | This is the pipe operator in `OCaml` and `F#`:
43 |
44 | let (|>) x f = f x
45 |
46 | and this is the translation I gave it for `Scala`:
47 |
48 | implicit final class MLPipe[T](val x: T) extends AnyVal {
49 | def |>[B](f: (T) => B) = f(x)
50 | }
51 |
52 | So, any value is lifted to an object having the pipe operator built-in.
53 |
54 | The implementation is deliberately kept *simple*, for now at least.
55 |
56 | Type inference as served from the compiler may not be your best friend at all times.
57 |
58 | Also, I could do away with some code duplication, although this does not bother me very much right now.
59 |
60 | Overall, the fun factor of doing this has been enormous.
61 |
62 | ### Motto
63 | > Less is more. Use both with a pipe.
64 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
20 |
21 | 4.0.0
22 |
23 | org.sonatype.oss
24 | oss-parent
25 | 7
26 |
27 |
28 | com.ckkloverdos
29 | scalapipes
30 | 0.2.0-SNAPSHOT
31 | jar
32 |
33 | Scala Pipes
34 |
35 | Scala collections with forward pipe operator, as in F# and OCaml.
36 |
37 | https://github.com/loverdos/scalapipes
38 | 2013
39 |
40 | ckkloverdos.com
41 | http://ckkloverdos.com
42 |
43 |
44 |
45 |
46 | Apache 2
47 | http://www.apache.org/licenses/LICENSE-2.0.txt
48 |
49 |
50 |
51 |
52 |
53 | loverdos
54 | Christos KK Loverdos
55 | loverdos@gmail.com
56 |
57 |
58 |
59 |
60 | https://loverdos@github.com/loverdos/scalapipes
61 | scm:git:https://loverdos@github.com/loverdos/scalapipes.git
62 | scm:git:git@github.com:loverdos/scalapipes.git
63 | HEAD
64 |
65 |
66 |
67 | Github
68 | https://github.com/loverdos/scalapipes/issues
69 |
70 |
71 |
72 | 2.10.2
73 | UTF-8
74 | ${project.build.sourceEncoding}
75 |
76 |
77 |
78 |
79 | org.scala-lang
80 | scala-library
81 | ${scala.version}
82 |
83 |
84 |
85 | junit
86 | junit
87 | 4.11
88 | test
89 |
90 |
91 |
92 |
93 | ${basedir}/src/main/scala
94 | ${basedir}/src/test/scala
95 |
96 |
97 | ${basedir}/src/main/resources
98 |
99 |
100 |
101 |
102 | ${basedir}/src/test/resources
103 |
104 |
105 |
106 |
107 | maven-enforcer-plugin
108 | 1.0.1
109 |
110 |
111 | enforce-tools
112 |
113 | enforce
114 |
115 |
116 |
117 |
118 | [1.6.0,)
119 |
120 |
121 | [3.0.3,)
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 | net.alchim31.maven
131 | scala-maven-plugin
132 | 3.1.6
133 |
134 | incremental
135 | ${scala.version}
136 | ${project.build.sourceEncoding}
137 |
138 | -Xlint:unchecked
139 | -Xlint:deprecation
140 |
141 |
142 | -deprecation
143 | -unchecked
144 | -optimize
145 |
146 |
147 |
148 |
149 | scala-compile-first
150 | process-resources
151 |
152 | add-source
153 | compile
154 | doc-jar
155 |
156 |
157 |
158 |
159 | scala-test-compile
160 | process-test-resources
161 |
162 |
163 | testCompile
164 |
165 |
166 |
167 |
168 |
169 |
170 | org.apache.maven.plugins
171 | maven-surefire-plugin
172 | 2.12.4
173 |
174 | false
175 | plain
176 |
177 |
178 |
179 |
180 | org.apache.maven.plugins
181 | maven-source-plugin
182 | 2.2.1
183 |
184 |
185 | attach-sources
186 |
187 | jar
188 |
189 | package
190 |
191 |
192 |
193 |
194 |
195 | org.apache.maven.plugins
196 | maven-release-plugin
197 | 2.3.2
198 |
199 | clean verify
200 |
201 |
202 |
203 |
204 | maven-repository-plugin
205 | 2.3.1
206 |
207 |
208 |
209 |
210 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/collection/generic/PMap.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes.collection.generic
18 |
19 | import scala.collection.Map
20 | import scala.collection.Seq
21 | import scala.collection.Set
22 |
23 | /**
24 | *
25 | * @author Christos KK Loverdos
26 | */
27 | object PMap {
28 | @inline final def filter[A, B](p: ((A, B)) ⇒ Boolean): Map[A, B] ⇒ Map[A, B] = _.filter(p)
29 |
30 | @inline final def map[A, B, C](f: ((A, B)) ⇒ C): Map[A, B] ⇒ Iterable[C] = _.map(f)
31 |
32 | @inline final def foreach[A, B](f: ((A, B)) ⇒ Unit): Map[A, B] ⇒ Unit = _.foreach(f)
33 |
34 | @inline final def foreachKey[A, B](f: (A) ⇒ Unit): Map[A, B] ⇒ Unit = _.keysIterator.foreach(f)
35 |
36 | @inline final def foreachValue[A, B](f: (B) ⇒ Unit): Map[A, B] ⇒ Unit = _.valuesIterator.foreach(f)
37 |
38 | @inline final def length[A, B]: Map[A, B] ⇒ Int = _.size
39 |
40 | @inline final def size[A, B]: Map[A, B] ⇒ Int = _.size
41 |
42 | @inline final def groupBy[A, B, K](f: ((A, B)) ⇒ K): Map[A, B] ⇒ Map[K, Map[A, B]] = _.groupBy(f)
43 |
44 | // ML-ish
45 | @inline final def iter[A, B](f: ((A, B)) ⇒ Unit): Map[A, B] ⇒ Unit = _.foreach(f)
46 |
47 | @inline final def ofSeq[A, B]: Seq[(A, B)] ⇒ Map[A, B] = _.toMap
48 |
49 | @inline final def ofSet[A, B]: Set[(A, B)] ⇒ Map[A, B] = _.toMap
50 |
51 | @inline final def ofList[A, B]: List[(A, B)] ⇒ Map[A, B] = _.toMap
52 |
53 | @inline final def ofIterable[A, B]: Iterable[(A, B)] ⇒ Map[A, B] = _.toMap
54 |
55 | @inline final def ofIterator[A, B]: Iterator[(A, B)] ⇒ Map[A, B] = _.toMap
56 |
57 | @inline final def ofMapMappingValues[A, B, C](vmap: (B) ⇒ C): scala.collection.Map[A, B] ⇒ Map[A, C] =
58 | _.map { case (k, v) ⇒ (k, vmap(v)) }. toMap
59 |
60 | @inline final def ofArray[A, B]: Array[(A, B)] ⇒ Map[A, B] = _.toMap
61 |
62 | @inline final def ofJava[K, V]: java.util.Map[K, V] ⇒ Map[K, V] = it ⇒ {
63 | import scala.collection.JavaConverters._
64 | it.asScala
65 | }
66 |
67 | @inline final def ofFuncWithInitialMap[K, V](initialMap: Map[K, V] = Map())(f: (K) ⇒ V): Map[K, V] =
68 | new Map[K, V] {
69 | private[this] var cachedMap = initialMap
70 |
71 | def get(key: K): Option[V] =
72 | cachedMap.get(key) match {
73 | case some@Some(value) ⇒
74 | some
75 |
76 | case None ⇒
77 | try {
78 | val value = f(key)
79 | cachedMap += key → value
80 | Some(value)
81 | }
82 | catch {
83 | case e: Throwable ⇒ None
84 | }
85 | }
86 |
87 |
88 | def iterator: Iterator[(K, V)] =
89 | cachedMap.iterator
90 |
91 | def -(key: K): Map[K, V] =
92 | ofFuncWithInitialMap(cachedMap - key)(f)
93 |
94 | def +[B1 >: V](kv: (K, B1)): Map[K, B1] =
95 | ofFuncWithInitialMap(cachedMap + kv)(f)
96 | }
97 |
98 | @inline final def ofFuncWithInitialKeys[K, V](allKeys: Set[K] = Set())(f: (K) ⇒ V): Map[K, V] =
99 | new Map[K, V] {
100 | private[this] var cachedMap = Map[K, V]((for(k ← allKeys) yield (k, f(k))).toSeq:_*)
101 |
102 | def get(key: K): Option[V] =
103 | cachedMap.get(key) match {
104 | case some@Some(value) ⇒
105 | some
106 |
107 | case None ⇒
108 | try {
109 | val value = f(key)
110 | cachedMap += key → value
111 | Some(value)
112 | }
113 | catch {
114 | case e: Throwable ⇒ None
115 | }
116 | }
117 |
118 | def iterator: Iterator[(K, V)] =
119 | cachedMap.iterator
120 |
121 | def -(key: K): Map[K, V] =
122 | ofFuncWithInitialMap(cachedMap - key)(f)
123 |
124 | def +[B1 >: V](kv: (K, B1)): Map[K, B1] =
125 | ofFuncWithInitialMap(cachedMap + kv)(f)
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/collection/generic/PSeq.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes.collection.generic
18 |
19 | import scala.collection.Map
20 | import scala.collection.Seq
21 |
22 | /**
23 | *
24 | * @author Christos KK Loverdos
25 | */
26 | object PSeq {
27 | @inline final def filter[A](p: (A) ⇒ Boolean): Seq[A] ⇒ Seq[A] = _.filter(p)
28 |
29 | @inline final def find[A](p: (A) ⇒ Boolean): Seq[A] ⇒ Option[A] = _.find(p)
30 |
31 | @inline final def filterDefined[A]: Seq[Option[A]] ⇒ Seq[A] = _.withFilter(_.isDefined).map(_.get)
32 |
33 | @inline final def map[A, B](f: (A) ⇒ B): Seq[A] ⇒ Seq[B] = _.map(f)
34 |
35 | @inline final def map_1[A]: Seq[(A, _)] ⇒ Seq[A] = _.map(_._1)
36 |
37 | @inline final def map_2[A]: Seq[(_, A)] ⇒ Seq[A] = _.map(_._2)
38 |
39 | @inline final def foreach[A](f: (A) ⇒ Unit): Seq[A] ⇒ Unit = _.foreach(f)
40 |
41 | @inline final def length[A]: Seq[A] ⇒ Int = _.length
42 |
43 | @inline final def size[A]: Seq[A] ⇒ Int = _.length
44 |
45 | @inline final def take[A](n: Int): Seq[A] ⇒ Seq[A] = _.take(n)
46 |
47 | @inline final def first[A]: Seq[A] ⇒ Option[A] = _.headOption
48 |
49 | @inline final def partition[A](f: (A) ⇒ Boolean): Seq[A] ⇒ (Seq[A], Seq[A]) = _.partition(f)
50 |
51 | @inline final def groupBy[A, K](f: (A) ⇒ K): Seq[A] ⇒ Map[K, Seq[A]] = _.groupBy(f)
52 |
53 | @inline final def mkString[A](sep: String): Seq[A] ⇒ String = _.mkString(sep)
54 |
55 | @inline final def mkString[A](start: String, sep: String, end: String): Seq[A] ⇒ String = _.mkString(start, sep, end)
56 |
57 | // This is for debugging
58 | @inline final def passThrough[A](f: (A) ⇒ Any): Seq[A] ⇒ Seq[A] = seq ⇒ {
59 | seq.foreach(f)
60 | seq
61 | }
62 |
63 | // ML-ish
64 | @inline final def iter[A](f: (A) ⇒ Unit): Seq[A] ⇒ Unit = _.foreach(f)
65 |
66 | @inline final def ofOne[A](x: A): Seq[A] = Seq(x)
67 |
68 | @inline final def ofIterable[A]: Iterable[A] ⇒ Seq[A] = _.toSeq
69 |
70 | @inline final def ofIterator[A]: Iterator[A] ⇒ Seq[A] = _.toSeq
71 |
72 | @inline final def ofList[A]: List[A] ⇒ Seq[A] = _.toSeq
73 |
74 | @inline final def ofArray[A]: Array[A] ⇒ Seq[A] = _.toSeq
75 |
76 | @inline final def ofMap[A, B]: Map[A, B] ⇒ Seq[(A, B)] = _.toSeq
77 |
78 | @inline final def ofMapSortedValuesBy[A, B, C](sortBy: (B) ⇒ C)(implicit ord: Ordering[C]): Map[A, B] ⇒ Seq[B] =
79 | it ⇒ Seq(it.toSeq.sortBy(kv ⇒ sortBy(kv._2)).map(_._2):_*)
80 |
81 | @inline final def ofMapFilteredValuesByKey[A, B](p: (A) ⇒ Boolean): Map[A, B] ⇒ Seq[B] =
82 | it ⇒ (for((k, v) ← it if p(k)) yield v).toSeq
83 |
84 | @inline final def ofMapValues[A, B]: Map[A, B] ⇒ Seq[B] = _.values.toSeq
85 |
86 | @inline final def ofJava[E]: java.util.Collection[E] ⇒ Seq[E] = it ⇒ {
87 | import scala.collection.JavaConverters._
88 | it.asScala.toSeq
89 | }
90 |
91 | @inline final def ofOption[A]: Option[A] ⇒ Seq[A] = {
92 | case Some(value) ⇒ Seq(value)
93 | case None ⇒ Seq()
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/collection/generic/PSet.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes.collection.generic
18 |
19 | import scala.collection.Map
20 | import scala.collection.Set
21 |
22 | /**
23 | *
24 | * @author Christos KK Loverdos
25 | */
26 | object PSet {
27 | @inline final def filter[A](p: (A) ⇒ Boolean): Set[A] ⇒ Set[A] = _.filter(p)
28 |
29 | @inline final def find[A](p: (A) ⇒ Boolean): Set[A] ⇒ Option[A] = _.find(p)
30 |
31 | @inline final def filterDefined[A]: Set[Option[A]] ⇒ Set[A] = _.withFilter(_.isDefined).map(_.get)
32 |
33 | @inline final def map[A, B](f: (A) ⇒ B): Set[A] ⇒ Set[B] = _.map(f)
34 |
35 | @inline final def map_1[A]: Set[(A, _)] ⇒ Set[A] = _.map(_._1)
36 |
37 | @inline final def map_2[A]: Set[(_, A)] ⇒ Set[A] = _.map(_._2)
38 |
39 | @inline final def foreach[A](f: (A) ⇒ Unit): Set[A] ⇒ Unit = _.foreach(f)
40 |
41 | @inline final def length[A]: Set[A] ⇒ Int = _.size
42 |
43 | @inline final def size[A]: Set[A] ⇒ Int = _.size
44 |
45 | @inline final def first[A]: Set[A] ⇒ Option[A] = _.headOption
46 |
47 | @inline final def partition[A](f: (A) ⇒ Boolean): Set[A] ⇒ (Set[A], Set[A]) = _.partition(f)
48 |
49 | @inline final def groupBy[A, K](f: (A) ⇒ K): Set[A] ⇒ Map[K, Set[A]] = _.groupBy(f)
50 |
51 | @inline final def mkString[A](sep: String): Set[A] ⇒ String = _.mkString(sep)
52 |
53 | @inline final def mkString[A](start: String, sep: String, end: String): Set[A] ⇒ String = _.mkString(start, sep, end)
54 |
55 | // This is for debugging
56 | @inline final def passThrough[A](f: (A) ⇒ Any): Set[A] ⇒ Set[A] = set ⇒ {
57 | set.foreach(f)
58 | set
59 | }
60 |
61 | // ML-ish
62 | @inline final def iter[A](f: (A) ⇒ Unit): Set[A] ⇒ Unit = _.foreach(f)
63 |
64 | @inline final def ofOne[A](x: A): Set[A] = Set(x)
65 |
66 | @inline final def ofIterable[A]: Iterable[A] ⇒ Set[A] = _.toSet
67 |
68 | @inline final def ofIterator[A]: Iterator[A] ⇒ Set[A] = _.toSet
69 |
70 | @inline final def ofSeq[A]: Seq[A] ⇒ Set[A] = _.toSet
71 |
72 | @inline final def ofList[A]: List[A] ⇒ Set[A] = _.toSet
73 |
74 | @inline final def ofArray[A]: Array[A] ⇒ Set[A] = _.toSet
75 |
76 | @inline final def ofMap[A, B]: Map[A, B] ⇒ Set[(A, B)] = _.toSet
77 |
78 | @inline final def ofSet[A]: Set[A] ⇒ Set[A] = identity
79 |
80 | @inline final def ofJava[E]: java.util.Set[E] ⇒ Set[E] = it ⇒ {
81 | import scala.collection.JavaConverters._
82 | it.asScala.to[Set]
83 | }
84 |
85 | @inline final def ofEnumSet[E <: Enum[E]](cls: Class[E]): Set[E] =
86 | ofJava(java.util.EnumSet.allOf(cls))
87 |
88 | @inline final def ofOption[A]: Option[A] ⇒ Set[A] = {
89 | case Some(value) ⇒ Set(value)
90 | case None ⇒ Set()
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/collection/immutable/PList.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes.collection.immutable
18 |
19 | import scala.collection.Iterable
20 |
21 | /**
22 | *
23 | * @author Christos KK Loverdos
24 | */
25 | object PList {
26 | @inline final def filter[A](p: (A) ⇒ Boolean): List[A] ⇒ List[A] = _.filter(p)
27 |
28 | @inline final def map[A, B](f: (A) ⇒ B): List[A] ⇒ List[B] = _.map(f)
29 |
30 | @inline final def foreach[A](f: (A) ⇒ Unit): List[A] ⇒ Unit = _.foreach(f)
31 |
32 | @inline final def length[A]: List[A] ⇒ Int = _.length
33 |
34 | @inline final def size[A]: List[A] ⇒ Int = _.length
35 |
36 | // ML-ish
37 | @inline final def iter[A](f: (A) ⇒ Unit): List[A] ⇒ Unit = _.foreach(f)
38 |
39 | @inline final def ofSeq[A]: Seq[A] ⇒ List[A] = _.toList
40 |
41 | @inline final def ofIterable[A]: Iterable[A] ⇒ List[A] = _.toList
42 |
43 | @inline final def ofArray[A]: Array[A] ⇒ List[A] = _.toList
44 |
45 | @inline final def ofMap[A, B]: Map[A, B] ⇒ List[(A, B)] = _.toList
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/collection/immutable/PMap.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes.collection.immutable
18 |
19 | import scala.collection.immutable.Map
20 | import scala.collection.Seq
21 | import scala.collection.Set
22 |
23 | /**
24 | *
25 | * @author Christos KK Loverdos
26 | */
27 | object PMap {
28 | @inline final def filter[A, B](p: ((A, B)) ⇒ Boolean): Map[A, B] ⇒ Map[A, B] = _.filter(p)
29 |
30 | @inline final def map[A, B, C](f: ((A, B)) ⇒ C): Map[A, B] ⇒ Iterable[C] = _.map(f)
31 |
32 | @inline final def foreach[A, B](f: ((A, B)) ⇒ Unit): Map[A, B] ⇒ Unit = _.foreach(f)
33 |
34 | @inline final def foreachKey[A, B](f: (A) ⇒ Unit): Map[A, B] ⇒ Unit = _.keysIterator.foreach(f)
35 |
36 | @inline final def foreachValue[A, B](f: (B) ⇒ Unit): Map[A, B] ⇒ Unit = _.valuesIterator.foreach(f)
37 |
38 | @inline final def length[A, B]: Map[A, B] ⇒ Int = _.size
39 |
40 | @inline final def size[A, B]: Map[A, B] ⇒ Int = _.size
41 |
42 | // ML-ish
43 | @inline final def iter[A, B](f: ((A, B)) ⇒ Unit): Map[A, B] ⇒ Unit = _.foreach(f)
44 |
45 | @inline final def ofSeq[A, B]: Seq[(A, B)] ⇒ Map[A, B] = _.toMap
46 |
47 | @inline final def ofSet[A, B]: Set[(A, B)] ⇒ Map[A, B] = _.toMap
48 |
49 | @inline final def ofList[A, B]: List[(A, B)] ⇒ Map[A, B] = _.toMap
50 |
51 | @inline final def ofIterable[A, B]: Iterable[(A, B)] ⇒ Map[A, B] = _.toMap
52 |
53 | @inline final def ofIterator[A, B]: Iterator[(A, B)] ⇒ Map[A, B] = _.toMap
54 |
55 | @inline final def ofMapMappingValues[A, B, C](vmap: (B) ⇒ C): scala.collection.Map[A, B] ⇒ Map[A, C] =
56 | _.map { case (k, v) ⇒ (k, vmap(v)) }. toMap
57 |
58 | @inline final def ofArray[A, B]: Array[(A, B)] ⇒ Map[A, B] = _.toMap
59 |
60 | @inline final def ofJava[K, V]: java.util.Map[K, V] ⇒ Map[K, V] = it ⇒ {
61 | import scala.collection.JavaConverters._
62 | Map(it.asScala.toSeq:_*)
63 | }
64 |
65 | @inline final def ofFuncWithInitialMap[K, V](initialMap: Map[K, V] = Map())(f: (K) ⇒ V): Map[K, V] =
66 | new Map[K, V] {
67 | private[this] var cachedMap = initialMap
68 |
69 | def get(key: K): Option[V] =
70 | cachedMap.get(key) match {
71 | case some@Some(value) ⇒
72 | some
73 |
74 | case None ⇒
75 | try {
76 | val value = f(key)
77 | cachedMap += key → value
78 | Some(value)
79 | }
80 | catch {
81 | case e: Throwable ⇒ None
82 | }
83 | }
84 |
85 |
86 | def iterator: Iterator[(K, V)] =
87 | cachedMap.iterator
88 |
89 | def -(key: K): Map[K, V] =
90 | ofFuncWithInitialMap(cachedMap - key)(f)
91 |
92 | def +[B1 >: V](kv: (K, B1)): Map[K, B1] =
93 | ofFuncWithInitialMap(cachedMap + kv)(f)
94 | }
95 |
96 | @inline final def ofFuncWithInitialKeys[K, V](allKeys: Set[K] = Set())(f: (K) ⇒ V): Map[K, V] =
97 | new Map[K, V] {
98 | private[this] var cachedMap = Map[K, V]((for(k ← allKeys) yield (k, f(k))).toSeq:_*)
99 |
100 | def get(key: K): Option[V] =
101 | cachedMap.get(key) match {
102 | case some@Some(value) ⇒
103 | some
104 |
105 | case None ⇒
106 | try {
107 | val value = f(key)
108 | cachedMap += key → value
109 | Some(value)
110 | }
111 | catch {
112 | case e: Throwable ⇒ None
113 | }
114 | }
115 |
116 | def iterator: Iterator[(K, V)] =
117 | cachedMap.iterator
118 |
119 | def -(key: K): Map[K, V] =
120 | ofFuncWithInitialMap(cachedMap - key)(f)
121 |
122 | def +[B1 >: V](kv: (K, B1)): Map[K, B1] =
123 | ofFuncWithInitialMap(cachedMap + kv)(f)
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/collection/immutable/POption.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes.collection.immutable
18 |
19 | import scala.collection.{Seq, Map, Set}
20 |
21 | /**
22 | *
23 | * @author Christos KK Loverdos
24 | */
25 | object POption {
26 | @inline final def filter[A](p: (A) ⇒ Boolean): Option[A] ⇒ Option[A] = _.filter(p)
27 |
28 | @inline final def getOr[A](default: ⇒A): Option[A] ⇒ A = _.getOrElse(default)
29 |
30 | @inline final def map[A, B](f: (A) ⇒ B): Option[A] ⇒ Option[B] = _.map(f)
31 |
32 | @inline final def map_1[A]: Option[(A, _)] ⇒ Option[A] = _.map(_._1)
33 |
34 | @inline final def map_2[A]: Option[(_, A)] ⇒ Option[A] = _.map(_._2)
35 |
36 | @inline final def flatMap[A, B](f: (A) ⇒ Option[B]): Option[A] ⇒ Option[B] = _.flatMap(f)
37 |
38 | @inline final def foreach[A](f: (A) ⇒ Unit): Option[A] ⇒ Unit = _.foreach(f)
39 |
40 | @inline final def length[A]: Option[A] ⇒ Int = size(_)
41 |
42 | @inline final def size[A]: Option[A] ⇒ Int = x ⇒ if(x.isDefined) 1 else 0
43 |
44 | @inline final def first[A]: Option[A] ⇒ Option[A] = identity
45 |
46 | // @inline final def flatten[A]: Traversable[Traversable[A]] ⇒ Option[A] = _.flatten.headOption
47 | @inline final def flatten[A]: Option[Option[A]] ⇒ Option[A] = _.flatten.headOption
48 |
49 | @inline final def mkString[A](sep: String): Option[A] ⇒ String = _.mkString(sep)
50 |
51 | @inline final def mkString[A](start: String, sep: String, end: String): Option[A] ⇒ String = _.mkString(start, sep, end)
52 |
53 | // ML-ish
54 | @inline final def iter[A](f: (A) ⇒ Unit): Option[A] ⇒ Unit = _.foreach(f)
55 |
56 | @inline final def ofIterable[A]: Iterable[A] ⇒ Option[A] = _.headOption
57 |
58 | @inline final def ofSeq[A]: Seq[A] ⇒ Option[A] = _.headOption
59 |
60 | @inline final def ofSet[A]: Set[A] ⇒ Option[A] = _.headOption
61 |
62 | @inline final def ofList[A]: List[A] ⇒ Option[A] = _.headOption
63 |
64 | @inline final def ofArray[A]: Array[A] ⇒ Option[A] = _.headOption
65 |
66 | @inline final def ofMap[A, B]: Map[A, B] ⇒ Option[(A, B)] = _.headOption
67 |
68 | @inline final def ofAny[A]: A ⇒ Option[A] = Option.apply
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/collection/immutable/PSeq.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes.collection.immutable
18 |
19 | import scala.collection.immutable.Seq
20 | import scala.collection.immutable.Map
21 |
22 | /**
23 | *
24 | * @author Christos KK Loverdos
25 | */
26 | object PSeq {
27 | @inline final def filter[A](p: (A) ⇒ Boolean): Seq[A] ⇒ Seq[A] = _.filter(p)
28 |
29 | @inline final def find[A](p: (A) ⇒ Boolean): Seq[A] ⇒ Option[A] = _.find(p)
30 |
31 | @inline final def filterDefined[A]: Seq[Option[A]] ⇒ Seq[A] = _.withFilter(_.isDefined).map(_.get)
32 |
33 | @inline final def map[A, B](f: (A) ⇒ B): Seq[A] ⇒ Seq[B] = _.map(f)
34 |
35 | @inline final def map_1[A]: Seq[(A, _)] ⇒ Seq[A] = _.map(_._1)
36 |
37 | @inline final def map_2[A]: Seq[(_, A)] ⇒ Seq[A] = _.map(_._2)
38 |
39 | @inline final def foreach[A](f: (A) ⇒ Unit): Seq[A] ⇒ Unit = _.foreach(f)
40 |
41 | @inline final def length[A]: Seq[A] ⇒ Int = _.length
42 |
43 | @inline final def size[A]: Seq[A] ⇒ Int = _.length
44 |
45 | @inline final def first[A]: Seq[A] ⇒ Option[A] = _.headOption
46 |
47 | @inline final def partition[A](f: (A) ⇒ Boolean): Seq[A] ⇒ (Seq[A], Seq[A]) = _.partition(f)
48 |
49 | @inline final def groupBy[A, K](f: (A) ⇒ K): Seq[A] ⇒ Map[K, Seq[A]] = _.groupBy(f)
50 |
51 | @inline final def mkString[A](sep: String): Seq[A] ⇒ String = _.mkString(sep)
52 |
53 | @inline final def mkString[A](start: String, sep: String, end: String): Seq[A] ⇒ String = _.mkString(start, sep, end)
54 |
55 | // This is for debugging
56 | @inline final def passThrough[A](f: (A) ⇒ Any): Seq[A] ⇒ Seq[A] = seq ⇒ {
57 | seq.foreach(f)
58 | seq
59 | }
60 |
61 | // ML-ish
62 | @inline final def iter[A](f: (A) ⇒ Unit): Seq[A] ⇒ Unit = _.foreach(f)
63 |
64 | @inline final def ofOne[A](x: A): Seq[A] = Seq(x)
65 |
66 | @inline final def ofIterable[A]: Iterable[A] ⇒ Seq[A] = _.to[Seq]
67 |
68 | @inline final def ofIterator[A]: Iterator[A] ⇒ Seq[A] = _.to[Seq]
69 |
70 | @inline final def ofList[A]: List[A] ⇒ Seq[A] = _.to[Seq]
71 |
72 | @inline final def ofArray[A]: Array[A] ⇒ Seq[A] = _.to[Seq]
73 |
74 | @inline final def ofMap[A, B]: Map[A, B] ⇒ Seq[(A, B)] = _.to[Seq]
75 |
76 | @inline final def ofMapSortedValuesBy[A, B, C](sortBy: (B) ⇒ C)(implicit ord: Ordering[C]): Map[A, B] ⇒ Seq[B] =
77 | it ⇒ Seq(it.toSeq.sortBy(kv ⇒ sortBy(kv._2)).map(_._2):_*)
78 |
79 | @inline final def ofMapFilteredValuesByKey[A, B](p: (A) ⇒ Boolean): Map[A, B] ⇒ Seq[B] =
80 | it ⇒ Seq((for((k, v) ← it if p(k)) yield v).toSeq:_*)
81 |
82 | @inline final def ofMapValues[A, B]: Map[A, B] ⇒ Seq[B] = it ⇒ Seq(it.values.toSeq:_*)
83 |
84 | @inline final def ofJava[E]: java.util.Collection[E] ⇒ Seq[E] = it ⇒ {
85 | import scala.collection.JavaConverters._
86 | it.asScala.to[Seq]
87 | }
88 |
89 | @inline final def ofOption[A]: Option[A] ⇒ Seq[A] = {
90 | case Some(value) ⇒ Seq(value)
91 | case None ⇒ Seq()
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/collection/immutable/PSet.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes.collection.immutable
18 |
19 | import scala.collection.immutable.Set
20 | import scala.collection.immutable.Seq
21 | import scala.collection.immutable.Map
22 |
23 | /**
24 | *
25 | * @author Christos KK Loverdos
26 | */
27 | object PSet {
28 | @inline final def filter[A](p: (A) ⇒ Boolean): Set[A] ⇒ Set[A] = _.filter(p)
29 |
30 | @inline final def find[A](p: (A) ⇒ Boolean): Set[A] ⇒ Option[A] = _.find(p)
31 |
32 | @inline final def filterDefined[A]: Set[Option[A]] ⇒ Set[A] = _.withFilter(_.isDefined).map(_.get)
33 |
34 | @inline final def map[A, B](f: (A) ⇒ B): Set[A] ⇒ Set[B] = _.map(f)
35 |
36 | @inline final def map_1[A]: Set[(A, _)] ⇒ Set[A] = _.map(_._1)
37 |
38 | @inline final def map_2[A]: Set[(_, A)] ⇒ Set[A] = _.map(_._2)
39 |
40 | @inline final def foreach[A](f: (A) ⇒ Unit): Set[A] ⇒ Unit = _.foreach(f)
41 |
42 | @inline final def length[A]: Set[A] ⇒ Int = _.size
43 |
44 | @inline final def size[A]: Set[A] ⇒ Int = _.size
45 |
46 | @inline final def first[A]: Set[A] ⇒ Option[A] = _.headOption
47 |
48 | @inline final def partition[A](f: (A) ⇒ Boolean): Set[A] ⇒ (Set[A], Set[A]) = _.partition(f)
49 |
50 | @inline final def groupBy[A, K](f: (A) ⇒ K): Set[A] ⇒ Map[K, Set[A]] = _.groupBy(f)
51 |
52 | @inline final def mkString[A](sep: String): Set[A] ⇒ String = _.mkString(sep)
53 |
54 | @inline final def mkString[A](start: String, sep: String, end: String): Set[A] ⇒ String = _.mkString(start, sep, end)
55 |
56 | // This is for debugging
57 | @inline final def passThrough[A](f: (A) ⇒ Any): Set[A] ⇒ Set[A] = set ⇒ {
58 | set.foreach(f)
59 | set
60 | }
61 |
62 | // ML-ish
63 | @inline final def iter[A](f: (A) ⇒ Unit): Set[A] ⇒ Unit = _.foreach(f)
64 |
65 | @inline final def ofOne[A](x: A): Set[A] = Set(x)
66 |
67 | @inline final def ofIterable[A]: Iterable[A] ⇒ Set[A] = _.toSet
68 |
69 | @inline final def ofIterator[A]: Iterator[A] ⇒ Set[A] = _.to[Set]
70 |
71 | @inline final def ofSeq[A]: Seq[A] ⇒ Set[A] = _.toSet
72 |
73 | @inline final def ofList[A]: List[A] ⇒ Set[A] = _.toSet
74 |
75 | @inline final def ofArray[A]: Array[A] ⇒ Set[A] = _.toSet
76 |
77 | @inline final def ofMap[A, B]: Map[A, B] ⇒ Set[(A, B)] = _.toSet
78 |
79 | @inline final def ofSet[A]: Set[A] ⇒ Set[A] = identity
80 |
81 | @inline final def ofJava[E]: java.util.Set[E] ⇒ Set[E] = it ⇒ {
82 | import scala.collection.JavaConverters._
83 | it.asScala.to[Set]
84 | }
85 |
86 | @inline final def ofEnumSet[E <: Enum[E]](cls: Class[E]): Set[E] =
87 | ofJava(java.util.EnumSet.allOf(cls))
88 |
89 | @inline final def ofOption[A]: Option[A] ⇒ Set[A] = {
90 | case Some(value) ⇒ Set(value)
91 | case None ⇒ Set()
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/collection/immutable/PStream.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes
18 | package collection.immutable
19 |
20 | /**
21 | *
22 | * @author Christos KK Loverdos
23 | */
24 | object PStream {
25 | @inline final def filter[A](p: (A) ⇒ Boolean): Stream[A] ⇒ Stream[A] = _.filter(p)
26 |
27 | @inline final def filterDefined[A]: Stream[Option[A]] ⇒ Stream[A] = _.withFilter(_.isDefined).map(_.get)
28 |
29 | @inline final def map[A, B](f: (A) ⇒ B): Stream[A] ⇒ Stream[B] = _.map(f)
30 |
31 | @inline final def map_1[A]: Stream[(A, _)] ⇒ Stream[A] = _.map(_._1)
32 |
33 | @inline final def map_2[A]: Stream[(_, A)] ⇒ Stream[A] = _.map(_._2)
34 |
35 | @inline final def foreach[A](f: (A) ⇒ Unit): Stream[A] ⇒ Unit = _.foreach(f)
36 |
37 | @inline final def length[A]: Stream[A] ⇒ Int = _.length
38 |
39 | @inline final def size[A]: Stream[A] ⇒ Int = _.length
40 |
41 | @inline final def first[A]: Stream[A] ⇒ Option[A] = _.headOption
42 |
43 | @inline final def partition[A](f: (A) ⇒ Boolean): Stream[A] ⇒ (Stream[A], Stream[A]) = _.partition(f)
44 |
45 | @inline final def groupBy[A, K](f: (A) ⇒ K): Stream[A] ⇒ Map[K, Stream[A]] = _.groupBy(f)
46 |
47 | @inline final def mkString[A](sep: String): Stream[A] ⇒ String = _.mkString(sep)
48 |
49 | @inline final def mkString[A](start: String, sep: String, end: String): Stream[A] ⇒ String = _.mkString(start, sep, end)
50 |
51 | // ML-ish
52 | @inline final def iter[A](f: (A) ⇒ Unit): Stream[A] ⇒ Unit = _.foreach(f)
53 |
54 | @inline final def ofOne[A](x: A): Stream[A] = Stream(x)
55 |
56 | @inline final def ofIterable[A]: Iterable[A] ⇒ Stream[A] = _.toStream
57 |
58 | @inline final def ofSeq[A]: Seq[A] ⇒ Stream[A] = _.toStream
59 |
60 | @inline final def ofList[A]: List[A] ⇒ Stream[A] = _.toStream
61 |
62 | @inline final def ofArray[A]: Array[A] ⇒ Stream[A] = _.toStream
63 |
64 | @inline final def ofMap[A, B]: Map[A, B] ⇒ Stream[(A, B)] = _.toStream
65 |
66 | @inline final def ofMapSortedValuesBy[A, B, C](sortBy: (B) ⇒ C)(implicit ord: Ordering[C]): Map[A, B] ⇒ Stream[B] =
67 | it ⇒ it |> PSeq.ofMapSortedValuesBy(sortBy) |> PStream.ofSeq
68 |
69 | @inline final def ofMapFilteredValuesByKey[A, B](p: (A) ⇒ Boolean): Map[A, B] ⇒ Stream[B] =
70 | it ⇒ (for((k, v) ← it if p(k)) yield v).toStream
71 |
72 | @inline final def ofMapValues[A, B]: Map[A, B] ⇒ Stream[B] = it ⇒ it.values.toStream
73 |
74 | @inline final def ofJava[E]: java.util.Collection[E] ⇒ Stream[E] = it ⇒ {
75 | import scala.collection.JavaConverters._
76 | it.asScala.toStream
77 | }
78 |
79 | @inline final def ofSeries[A](n0: Int): ((Int) ⇒ A) ⇒ Stream[A] =
80 | f ⇒ Stream.cons(f(n0), ofSeries(n0 + 1)(f))
81 |
82 | @inline final def ofOption[A]: Option[A] ⇒ Stream[A] = {
83 | case Some(value) ⇒ Stream(value)
84 | case None ⇒ Stream()
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/collection/mutable/JList.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes.collection.mutable
18 |
19 | import scala.collection.JavaConverters._
20 | import scala.collection.Seq
21 | import java.{util ⇒ ju}
22 |
23 | /**
24 | *
25 | * @author Christos KK Loverdos
26 | */
27 | object JList {
28 | @inline final def ofSeq[A]: Seq[A] ⇒ ju.List[A] = _.asJava
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/collection/mutable/PSeq.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes.collection.mutable
18 |
19 | import scala.collection.mutable.Seq
20 | import scala.collection.mutable.Map
21 |
22 | /**
23 | *
24 | * @author Christos KK Loverdos
25 | */
26 | object PSeq {
27 | @inline final def filter[A](p: (A) ⇒ Boolean): Seq[A] ⇒ Seq[A] = _.filter(p)
28 |
29 | @inline final def find[A](p: (A) ⇒ Boolean): Seq[A] ⇒ Option[A] = _.find(p)
30 |
31 | @inline final def filterDefined[A]: Seq[Option[A]] ⇒ Seq[A] = _.withFilter(_.isDefined).map(_.get)
32 |
33 | @inline final def map[A, B](f: (A) ⇒ B): Seq[A] ⇒ Seq[B] = _.map(f)
34 |
35 | @inline final def map_1[A]: Seq[(A, _)] ⇒ Seq[A] = _.map(_._1)
36 |
37 | @inline final def map_2[A]: Seq[(_, A)] ⇒ Seq[A] = _.map(_._2)
38 |
39 | @inline final def foreach[A](f: (A) ⇒ Unit): Seq[A] ⇒ Unit = _.foreach(f)
40 |
41 | @inline final def length[A]: Seq[A] ⇒ Int = _.length
42 |
43 | @inline final def size[A]: Seq[A] ⇒ Int = _.length
44 |
45 | @inline final def first[A]: Seq[A] ⇒ Option[A] = _.headOption
46 |
47 | @inline final def partition[A](f: (A) ⇒ Boolean): Seq[A] ⇒ (Seq[A], Seq[A]) = _.partition(f)
48 |
49 | @inline final def mkString[A](sep: String): Seq[A] ⇒ String = _.mkString(sep)
50 |
51 | @inline final def mkString[A](start: String, sep: String, end: String): Seq[A] ⇒ String = _.mkString(start, sep, end)
52 |
53 | // This is for debugging
54 | @inline final def passThrough[A](f: (A) ⇒ Any): Seq[A] ⇒ Seq[A] = seq ⇒ {
55 | seq.foreach(f)
56 | seq
57 | }
58 |
59 | // ML-ish
60 | @inline final def iter[A](f: (A) ⇒ Unit): Seq[A] ⇒ Unit = _.foreach(f)
61 |
62 | @inline final def ofOne[A](x: A): Seq[A] = Seq(x)
63 |
64 | @inline final def ofIterable[A]: Iterable[A] ⇒ Seq[A] = _.to[Seq]
65 |
66 | @inline final def ofIterator[A]: Iterator[A] ⇒ Seq[A] = _.to[Seq]
67 |
68 | @inline final def ofList[A]: List[A] ⇒ Seq[A] = _.to[Seq]
69 |
70 | @inline final def ofArray[A]: Array[A] ⇒ Seq[A] = _.to[Seq]
71 |
72 | @inline final def ofMap[A, B]: Map[A, B] ⇒ Seq[(A, B)] = _.to[Seq]
73 |
74 | @inline final def ofMapSortedValuesBy[A, B, C](sortBy: (B) ⇒ C)(implicit ord: Ordering[C]): Map[A, B] ⇒ Seq[B] =
75 | it ⇒ Seq(it.toSeq.sortBy(kv ⇒ sortBy(kv._2)).map(_._2):_*)
76 |
77 | @inline final def ofMapFilteredValuesByKey[A, B](p: (A) ⇒ Boolean): Map[A, B] ⇒ Seq[B] =
78 | it ⇒ Seq((for((k, v) ← it if p(k)) yield v).toSeq:_*)
79 |
80 | @inline final def ofMapValues[A, B]: Map[A, B] ⇒ Seq[B] = it ⇒ Seq(it.values.toSeq:_*)
81 |
82 | @inline final def ofJava[E]: java.util.Collection[E] ⇒ Seq[E] = it ⇒ {
83 | import scala.collection.JavaConverters._
84 | it.asScala.to[Seq]
85 | }
86 |
87 | @inline final def ofOption[A]: Option[A] ⇒ Seq[A] = {
88 | case Some(value) ⇒ Seq(value)
89 | case None ⇒ Seq()
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/collection/mutable/PSet.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes.collection.mutable
18 |
19 | import scala.collection.mutable.Set
20 |
21 | /**
22 | *
23 | * @author Christos KK Loverdos
24 | */
25 | object PSet {
26 | @inline final def filter[A](p: (A) ⇒ Boolean): Set[A] ⇒ Set[A] = _.filter(p)
27 |
28 | @inline final def find[A](p: (A) ⇒ Boolean): Set[A] ⇒ Option[A] = _.find(p)
29 |
30 | @inline final def filterDefined[A]: Set[Option[A]] ⇒ Set[A] = _.withFilter(_.isDefined).map(_.get)
31 |
32 | @inline final def map[A, B](f: (A) ⇒ B): Set[A] ⇒ Set[B] = _.map(f)
33 |
34 | @inline final def map_1[A]: Set[(A, _)] ⇒ Set[A] = _.map(_._1)
35 |
36 | @inline final def map_2[A]: Set[(_, A)] ⇒ Set[A] = _.map(_._2)
37 |
38 | @inline final def foreach[A](f: (A) ⇒ Unit): Set[A] ⇒ Unit = _.foreach(f)
39 |
40 | @inline final def length[A]: Set[A] ⇒ Int = _.size
41 |
42 | @inline final def size[A]: Set[A] ⇒ Int = _.size
43 |
44 | @inline final def first[A]: Set[A] ⇒ Option[A] = _.headOption
45 |
46 | @inline final def partition[A](f: (A) ⇒ Boolean): Set[A] ⇒ (Set[A], Set[A]) = _.partition(f)
47 |
48 | @inline final def groupBy[A, K](f: (A) ⇒ K): Set[A] ⇒ Map[K, Set[A]] = _.groupBy(f)
49 |
50 | @inline final def mkString[A](sep: String): Set[A] ⇒ String = _.mkString(sep)
51 |
52 | @inline final def mkString[A](start: String, sep: String, end: String): Set[A] ⇒ String = _.mkString(start, sep, end)
53 |
54 | // This is for debugging
55 | @inline final def passThrough[A](f: (A) ⇒ Any): Set[A] ⇒ Set[A] = set ⇒ {
56 | set.foreach(f)
57 | set
58 | }
59 |
60 | // ML-ish
61 | @inline final def iter[A](f: (A) ⇒ Unit): Set[A] ⇒ Unit = _.foreach(f)
62 |
63 | @inline final def ofOne[A](x: A): Set[A] = Set(x)
64 |
65 | @inline final def ofIterable[A]: Iterable[A] ⇒ Set[A] = _.to[Set]
66 |
67 | @inline final def ofIterator[A]: Iterator[A] ⇒ Set[A] = _.to[Set]
68 |
69 | @inline final def ofSeq[A]: Seq[A] ⇒ Set[A] = _.to[Set]
70 |
71 | @inline final def ofList[A]: List[A] ⇒ Set[A] = _.to[Set]
72 |
73 | @inline final def ofArray[A]: Array[A] ⇒ Set[A] = _.to[Set]
74 |
75 | @inline final def ofMap[A, B]: Map[A, B] ⇒ Set[(A, B)] = _.to[Set]
76 |
77 | @inline final def ofSet[A]: Set[A] ⇒ Set[A] = identity
78 |
79 | @inline final def ofJava[E]: java.util.Set[E] ⇒ Set[E] = it ⇒ {
80 | import scala.collection.JavaConverters._
81 | it.asScala.to[Set]
82 | }
83 |
84 | @inline final def ofEnumSet[E <: Enum[E]](cls: Class[E]): Set[E] =
85 | ofJava(java.util.EnumSet.allOf(cls))
86 |
87 | @inline final def ofOption[A]: Option[A] ⇒ Set[A] = {
88 | case Some(value) ⇒ Set(value)
89 | case None ⇒ Set()
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/concurrent/PFuture.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes
18 | package concurrent
19 |
20 | import scala.concurrent.duration.Duration
21 | import scala.concurrent.{Await, Promise, ExecutionContext, Future}
22 |
23 | /**
24 | *
25 | * @author Christos KK Loverdos
26 | */
27 | object PFuture {
28 | @inline final def filter[A](p: (A) ⇒ Boolean)
29 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Future[A] ⇒ Future[A] =
30 | _.filter(p)
31 |
32 | @inline final def filterSeq[A](p: (A) ⇒ Boolean)
33 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Future[Seq[A]] ⇒ Future[Seq[A]] =
34 | _.map(_.filter(p))
35 |
36 | @inline final def foreach[A](f: (A) ⇒ Unit)
37 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Future[A] ⇒ Unit =
38 | _.foreach(f)
39 |
40 | @inline final def foreachSeq[A](f: (A) ⇒ Unit)
41 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Future[Seq[A]] ⇒ Unit =
42 | _.foreach(_.foreach(f))
43 |
44 | @inline final def filterDefined[A]
45 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Future[Option[A]] ⇒ Future[A] =
46 | _.withFilter(_.isDefined).map(_.get)
47 |
48 | @inline final def filterSeqDefined[A]
49 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Future[Seq[Option[A]]] ⇒ Future[Seq[A]] =
50 | _.map(ISeq.filterDefined)
51 |
52 | @inline final def map[A, B](f: (A) ⇒ B)
53 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Future[A] ⇒ Future[B] =
54 | _.map(f)
55 |
56 | @inline final def mapSeq[A, B](f: (A) ⇒ B)
57 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Future[Seq[A]] ⇒ Future[Seq[B]] =
58 | _.map(_.map(f))
59 |
60 | @inline final def flatMap[A, B](f: (A) ⇒ Future[B])
61 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Future[A] ⇒ Future[B] =
62 | _.flatMap(f)
63 |
64 | @inline final def awaitCompletion[A](atMost: Duration)
65 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Future[A] ⇒ Future[A] =
66 | Await.ready(_, atMost)
67 |
68 | @inline final def awaitResult[A](atMost: Duration): Future[A] ⇒ A =
69 | Await.result(_, atMost)
70 |
71 | // ML-ish
72 | @inline final def ofValue[A]: A ⇒ Future[A] = Future.successful
73 |
74 | @inline final def ofComputation[A]
75 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): (⇒A) ⇒ Future[A] =
76 | Future(_)
77 |
78 | @inline final def ofPromise[A]: Promise[A] ⇒ Future[A] = _.future
79 |
80 | @inline final def ofSeqFuture[A]
81 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Seq[Future[A]] ⇒ Future[Seq[A]] =
82 | Future.sequence[A, Seq]
83 |
84 | @inline final def iter[A](f: (A) ⇒ Unit)
85 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Future[A] ⇒ Unit =
86 | _.foreach(f)
87 |
88 | @inline final def iterSeq[A](f: (A) ⇒ Unit)
89 | (implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Future[Seq[A]] ⇒ Unit =
90 | _.foreach(_.foreach(f))
91 | }
92 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/concurrent/PPromise.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos.pipes.concurrent
18 |
19 | import scala.concurrent.Future
20 | import scala.concurrent.Promise
21 |
22 | /**
23 | *
24 | * @author Christos KK Loverdos
25 | */
26 | object PPromise {
27 | @inline final def ofValue[T]: T ⇒ Promise[T] = Promise.successful
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/scala/com/ckkloverdos/pipes/package.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2013 Christos KK Loverdos
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.ckkloverdos
18 |
19 | import com.ckkloverdos.pipes.collection.{generic, mutable, immutable}
20 |
21 | /**
22 | * Scala collections with forward pipe operator, as in F# and OCaml.
23 | * No batteries included.
24 | *
25 | * @author Christos KK Loverdos
26 | */
27 | package object pipes {
28 | // let (|>) x f = f x
29 | implicit final class MLPipe[T](val x: T) extends AnyVal {
30 | def |>[B](f: (T) ⇒ B) = f(x)
31 | }
32 |
33 | // Fix Predef
34 | final type Seq[+A] = scala.collection.immutable.Seq[A]
35 | final val Seq = scala.collection.immutable.Seq
36 |
37 | final val JList = mutable.JList
38 |
39 | final val IOption = immutable.POption
40 |
41 | final val IFuture = concurrent.PFuture
42 | final val IPromise = concurrent.PPromise
43 |
44 | final val ISeq = immutable.PSeq
45 | final val IStream = immutable.PStream
46 | final val ISet = immutable.PSet
47 | final val IMap = immutable.PMap
48 |
49 | final val MSeq = mutable.PSeq
50 | final val MSet = mutable.PSet
51 | // val MMap = mutable.PMap
52 |
53 | private[this] object tests {
54 | val one = 1
55 | val two = one |> (_ + 1)
56 | val three = two |> (_ + 1)
57 | val threeAgain = one |> (_ + 1) |> (_ + 1)
58 |
59 | val letters = Seq('a', 'b', 'c', 'd', 'a')
60 | val As = letters |> generic.PSeq.filter(ch ⇒ ch == 'a')
61 | val As_ = letters |> generic.PSeq.filter(_ == 'a')
62 | val AsLen = As |> generic.PSeq.length
63 | val AsLenPlusOne = As |>
64 | generic.PSeq.length |>
65 | (_ + 1)
66 |
67 | val numbers = IndexedSeq(1, 2, 3, 4, 5, 6)
68 | val lessThanFour = numbers |> generic.PSeq.filter(_ < 4)
69 |
70 | val map = Map(1 → "one", 2 → "two", 3 → "three")
71 | val incKeys = map |>
72 | generic.PMap.map {case (k, v) ⇒ k } |>
73 | generic.PSeq.ofIterable
74 | }
75 |
76 |
77 | val keys = Map(1 → "keyA", 2 → "keyB", 3 → "keyC")
78 | val newKeys = keys.map { case (k, v) ⇒ (k + 1, v) }
79 | val keysPlus = keys |>
80 | generic.PMap.map { case (k, v) ⇒ (k + 1, v) } |>
81 | generic.PMap.ofIterable
82 | }
83 |
--------------------------------------------------------------------------------