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

PartialFunction + match(case ..) statements #143

Merged
merged 12 commits into from
Jul 24, 2017
Merged

Conversation

raulraja
Copy link
Member

@raulraja raulraja commented Jul 24, 2017

The following PR brings supports to PartialFunctions for Kotlin through Kategory.
It includes syntax for match and case and extensions for Iterable.collect so they work like in Scala.

listOf("1", Option.Some(1), 1).collect(
  case(typeOf<String>() then { (it).toInt() }),
  case(typeOf<Option.Some<Int>>() then { it.value }),
  case(typeOf<Int>() then { it })
) shouldBe listOf(1, 1, 1)
listOf(1).collect(
  case(typeOf<String>() then { (it).toInt() })
) shouldBe emptyList<Int>()
val result: Int = match("1")(
  case(typeOf<String>() then { (it).toInt() }),
  case(typeOf<Option.Some<Int>>() then { it.value }),
  case(typeOf<Int>() then { it }),
  default = { it.toInt() }
)
result shouldBe 1

@codecov-io
Copy link

codecov-io commented Jul 24, 2017

Codecov Report

❗ No coverage uploaded for pull request base (master@07f0af7). Click here to learn what that means.
The diff coverage is 50%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #143   +/-   ##
=========================================
  Coverage          ?   55.46%           
  Complexity        ?      170           
=========================================
  Files             ?       71           
  Lines             ?     1509           
  Branches          ?      181           
=========================================
  Hits              ?      837           
  Misses            ?      593           
  Partials          ?       79
Impacted Files Coverage Δ Complexity Δ
.../src/main/kotlin/kategory/arrow/PartialFunction.kt 36.84% <36.84%> (ø) 1 <1> (?)
...ain/kotlin/kategory/instances/IterableInstances.kt 72.72% <72.72%> (ø) 0 <0> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 07f0af7...4854416. Read the comment docs.

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we don't use these per file anymore.

Copy link
Member

@ffgiraldez ffgiraldez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a like question, but just to clarify me.

package kategory

@Suppress("UNCHECKED_CAST")
fun <A: Any, B> Iterable<A>.collect(vararg cases: PartialFunction<*, B>): List<B> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's ok, handle Iterables and return a List? It's mandatory that collect() return a List or it could return something more generic like Iterable or Collection?

result shouldBe 1
}

"match nested statements" {
Copy link
Member

@ffgiraldez ffgiraldez Jul 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Travis point that this function does not pass the test PartialFunctionTests.match nested statements FAILED java.lang.ClassCastException

could you take a look @pakoito?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I proposed this test because I suspected it wouldn't pass. @raulraja is on it!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hahaha, Ok good to know.

Try({ it as A }).fold({ false }, { true })
}

infix fun <A, B> ((A) -> Boolean).then(f: (A) -> B): Tuple2<(A) -> Boolean, (A) -> B> = Tuple2(this, f)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the functions then, typeof, case should be scoped to the class match or PartialFuncion so they don't scape the DSL.

Copy link
Member Author

@raulraja raulraja Jul 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that for those to be scoped they would need to be placed inside the companion object and would have to be individually imported:

import kategory.match.dsl.case
import kategory.match.dsl.typeOf
...

I have never been able to import members of companions with wildcards in Kotlin and it feels to me that those extra imports are an unnecessary burden to the user. Also this will be the first time we require additional imports beside kategory.* are we sure we want to do that?

Alternatively I can create a new kategory.cases package for these functions but also the users would have to explicitly import import kategory.cases.*

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Yes, let's discuss whether this is part of the main package or an extension some other time. It's :shipit: time!

Copy link
Member

@pakoito pakoito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for this feature many people are going to consider the library :D

@raulraja raulraja merged commit 98f407e into master Jul 24, 2017
@raulraja raulraja deleted the rr-partial-functions branch July 24, 2017 22:03
@raulraja raulraja mentioned this pull request Jul 25, 2017
54 tasks
rachelcarmena pushed a commit that referenced this pull request Feb 24, 2021
From the gradle docs:
The compile configuration still exists but should not be used as it will not offer the guarantees that the api and implementation configurations provide.

https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_separation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants