Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
boolinq
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
boolinq
Commits
a9bd4b6d
Commit
a9bd4b6d
authored
Sep 05, 2019
by
Anton Bukov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add optional defaultValue argument for firstOrDefault and lastOrDefault
parent
1d09dc8b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
8 deletions
+23
-8
boolinq.h
include/boolinq/boolinq.h
+8
-8
FirstTest.cpp
test/FirstTest.cpp
+8
-0
LastTest.cpp
test/LastTest.cpp
+7
-0
No files found.
include/boolinq/boolinq.h
View file @
a9bd4b6d
...
@@ -540,22 +540,22 @@ namespace boolinq {
...
@@ -540,22 +540,22 @@ namespace boolinq {
return
Linq
<
S
,
T
>
(
*
this
).
next
();
return
Linq
<
S
,
T
>
(
*
this
).
next
();
}
}
T
firstOrDefault
(
std
::
function
<
bool
(
T
)
>
predicate
)
const
T
firstOrDefault
(
std
::
function
<
bool
(
T
)
>
predicate
,
T
defaultValue
=
T
()
)
const
{
{
try
{
try
{
return
where
(
predicate
).
next
();
return
where
(
predicate
).
next
();
}
}
catch
(
LinqEndException
&
)
{}
catch
(
LinqEndException
&
)
{}
return
T
()
;
return
defaultValue
;
}
}
T
firstOrDefault
()
const
T
firstOrDefault
(
T
defaultValue
=
T
()
)
const
{
{
try
{
try
{
return
Linq
<
S
,
T
>
(
*
this
).
next
();
return
Linq
<
S
,
T
>
(
*
this
).
next
();
}
}
catch
(
LinqEndException
&
)
{}
catch
(
LinqEndException
&
)
{}
return
T
()
;
return
defaultValue
;
}
}
T
last
(
std
::
function
<
bool
(
T
)
>
predicate
)
const
T
last
(
std
::
function
<
bool
(
T
)
>
predicate
)
const
...
@@ -578,18 +578,18 @@ namespace boolinq {
...
@@ -578,18 +578,18 @@ namespace boolinq {
return
last
([](
T
/*value*/
)
{
return
true
;
});
return
last
([](
T
/*value*/
)
{
return
true
;
});
}
}
T
lastOrDefault
(
std
::
function
<
bool
(
T
)
>
predicate
)
const
T
lastOrDefault
(
std
::
function
<
bool
(
T
)
>
predicate
,
T
defaultValue
=
T
()
)
const
{
{
T
res
=
T
()
;
T
res
=
defaultValue
;
where
(
predicate
).
for_each
([
&
res
](
T
value
)
{
where
(
predicate
).
for_each
([
&
res
](
T
value
)
{
res
=
value
;
res
=
value
;
});
});
return
res
;
return
res
;
}
}
T
lastOrDefault
()
const
T
lastOrDefault
(
T
defaultValue
=
T
()
)
const
{
{
return
lastOrDefault
([](
T
/*value*/
)
{
return
true
;
});
return
lastOrDefault
([](
T
/*value*/
)
{
return
true
;
}
,
defaultValue
);
}
}
// Export to containers
// Export to containers
...
...
test/FirstTest.cpp
View file @
a9bd4b6d
...
@@ -37,3 +37,11 @@ TEST(First, NoneElements)
...
@@ -37,3 +37,11 @@ TEST(First, NoneElements)
EXPECT_EQ
(
0
,
from
(
src
).
firstOrDefault
());
EXPECT_EQ
(
0
,
from
(
src
).
firstOrDefault
());
EXPECT_EQ
(
0
,
from
(
src
).
firstOrDefault
([](
int
a
){
return
a
%
2
==
0
;}));
EXPECT_EQ
(
0
,
from
(
src
).
firstOrDefault
([](
int
a
){
return
a
%
2
==
0
;}));
}
}
TEST
(
First
,
NoneElementsWithArg
)
{
std
::
vector
<
int
>
src
=
{};
EXPECT_EQ
(
2
,
from
(
src
).
firstOrDefault
(
2
));
EXPECT_EQ
(
3
,
from
(
src
).
firstOrDefault
([](
int
a
){
return
a
%
2
==
0
;},
3
));
}
test/LastTest.cpp
View file @
a9bd4b6d
...
@@ -37,3 +37,10 @@ TEST(Last, NoneElements)
...
@@ -37,3 +37,10 @@ TEST(Last, NoneElements)
EXPECT_EQ
(
0
,
from
(
src
).
lastOrDefault
());
EXPECT_EQ
(
0
,
from
(
src
).
lastOrDefault
());
EXPECT_EQ
(
0
,
from
(
src
).
lastOrDefault
([](
int
a
){
return
a
%
2
==
0
;}));
EXPECT_EQ
(
0
,
from
(
src
).
lastOrDefault
([](
int
a
){
return
a
%
2
==
0
;}));
}
}
TEST
(
Last
,
NoneElementsWithArgument
)
{
std
::
vector
<
int
>
src
=
{};
EXPECT_EQ
(
2
,
from
(
src
).
lastOrDefault
(
2
));
EXPECT_EQ
(
3
,
from
(
src
).
lastOrDefault
([](
int
a
)
{
return
a
%
2
==
0
;
},
3
));
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment