Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LARA
inox
Commits
618c9599
Commit
618c9599
authored
9 years ago
by
Manos Koukoutos
Browse files
Options
Downloads
Patches
Plain Diff
Unapply in docs, List. List.headOption
parent
4a123c76
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
doc/purescala.rst
+26
-10
26 additions, 10 deletions
doc/purescala.rst
library/collection/List.scala
+14
-0
14 additions, 0 deletions
library/collection/List.scala
library/lang/string/package.scala
+1
-1
1 addition, 1 deletion
library/lang/string/package.scala
with
41 additions
and
11 deletions
doc/purescala.rst
+
26
−
10
View file @
618c9599
...
...
@@ -182,16 +182,32 @@ Pattern matching
.. code-block:: scala
expr match {
// Simple (nested) patterns:
case CaseClass( .. , .. , ..) => ...
case v @ CaseClass( .. , .. , ..) => ...
case v : CaseClass => ...
case (t1, t2) => ...
case 42 => ...
case _ => ...
// can also be guarded, e.g.
case CaseClass(a, b, c) if a > b => ...
// Simple (nested) patterns:
case CaseClass( .. , .. , ..) => ...
case v @ CaseClass( .. , .. , ..) => ...
case v : CaseClass => ...
case (t1, t2) => ...
case 42 => ...
case _ => ...
// can also be guarded, e.g.
case CaseClass(a, b, c) if a > b => ...
}
Custom pattern matching with ``unapply`` methods are also supported:
.. code-block:: scala
object :: {
def unapply[A](l: List[A]): Option[(A, List[A])] = l match {
case Nil() => None()
case Cons(x, xs) => Some((x, xs))
}
}
def empty[A](l: List[A]) = l match {
case x :: xs => false
case Nil() => true
}
Values
...
...
This diff is collapsed.
Click to expand it.
library/collection/List.scala
+
14
−
0
View file @
618c9599
...
...
@@ -35,6 +35,11 @@ sealed abstract class List[T] {
(
that
!=
Nil
[
T
]()
||
res
==
this
)
}
def
headOption
:
Option
[
T
]
=
this
match
{
case
Nil
()
=>
None
[
T
]()
case
Cons
(
h
,
_
)
=>
Some
(
h
)
}
def
head
:
T
=
{
require
(
this
!=
Nil
[
T
]())
val
Cons
(
h
,
_
)
=
this
...
...
@@ -537,6 +542,15 @@ object ListOps {
case
class
Cons
[
T
](
h
:
T
,
t
:
List
[
T
])
extends
List
[
T
]
case
class
Nil
[
T
]()
extends
List
[
T
]
// 'Cons' Extractor
object
::
{
def
unapply
[
A
](
l
:
List
[
A
])
:
Option
[(
A
,
List
[
A
])]
=
l
match
{
case
Nil
()
=>
None
()
case
Cons
(
x
,
xs
)
=>
Some
((
x
,
xs
))
}
}
@library
object
ListSpecs
{
def
snocIndex
[
T
](
l
:
List
[
T
],
t
:
T
,
i
:
BigInt
)
:
Boolean
=
{
...
...
This diff is collapsed.
Click to expand it.
library/lang/string/package.scala
+
1
−
1
View file @
618c9599
...
...
@@ -16,7 +16,7 @@ package object string {
@ignore
def
listToList
[
A
](
s
:
ScalaList
[
A
])
:
List
[
A
]
=
s
match
{
case
h
::
t
=>
case
scala
.::(
h
,
t
)
=>
Cons
(
h
,
listToList
(
t
))
case
_
=>
Nil
[
A
]()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment