Skip to content

Commit 14750f7

Browse files
committed
Add split_evenly function
1 parent 0b5d720 commit 14750f7

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

include/fplus/curry_instances.autogenerated_defines

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ fplus_curry_define_fn_2(insert_at_idx)
394394
fplus_curry_define_fn_1(partition)
395395
fplus_curry_define_fn_1(split_at_idxs)
396396
fplus_curry_define_fn_1(split_every)
397+
fplus_curry_define_fn_1(split_evenly)
397398
fplus_curry_define_fn_2(split_by_token)
398399
fplus_curry_define_fn_1(run_length_encode_by)
399400
fplus_curry_define_fn_0(run_length_encode)

include/fplus/fwd_instances.autogenerated_defines

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ fplus_fwd_define_fn_2(insert_at_idx)
394394
fplus_fwd_define_fn_1(partition)
395395
fplus_fwd_define_fn_1(split_at_idxs)
396396
fplus_fwd_define_fn_1(split_every)
397+
fplus_fwd_define_fn_1(split_evenly)
397398
fplus_fwd_define_fn_2(split_by_token)
398399
fplus_fwd_define_fn_1(run_length_encode_by)
399400
fplus_fwd_define_fn_0(run_length_encode)
@@ -693,6 +694,7 @@ fplus_fwd_flip_define_fn_1(split_at_idx)
693694
fplus_fwd_flip_define_fn_1(partition)
694695
fplus_fwd_flip_define_fn_1(split_at_idxs)
695696
fplus_fwd_flip_define_fn_1(split_every)
697+
fplus_fwd_flip_define_fn_1(split_evenly)
696698
fplus_fwd_flip_define_fn_1(run_length_encode_by)
697699
fplus_fwd_flip_define_fn_1(span)
698700
fplus_fwd_flip_define_fn_1(aperture)

include/fplus/split.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,24 @@ ContainerOut split_every(std::size_t n, const ContainerIn& xs)
492492
xs);
493493
}
494494

495+
// API search type: split_evenly : (Int, [a]) -> [[a]]
496+
// fwd bind count: 1
497+
// Split a sequence into n similarly-sized chunks.
498+
// split_evenly(2, [0,1,2,3,4]) == [[0,1,2],[3,4]]
499+
template <typename ContainerIn,
500+
typename ContainerOut = std::vector<ContainerIn>>
501+
ContainerOut split_evenly(std::size_t n, const ContainerIn& xs)
502+
{
503+
const std::size_t every_n = size_of_cont(xs) / n;
504+
return split_at_idxs<
505+
std::vector<std::size_t>,
506+
ContainerIn,
507+
ContainerOut>(
508+
numbers_step<std::size_t>(
509+
every_n, size_of_cont(xs), every_n),
510+
xs);
511+
}
512+
495513
// API search type: split_by_token : ([a], Bool, [a]) -> [[a]]
496514
// fwd bind count: 2
497515
// Split a sequence at every segment matching a token.

include_all_in_one/include/fplus/fplus.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10762,6 +10762,24 @@ ContainerOut split_every(std::size_t n, const ContainerIn& xs)
1076210762
xs);
1076310763
}
1076410764

10765+
// API search type: split_evenly : (Int, [a]) -> [[a]]
10766+
// fwd bind count: 1
10767+
// Split a sequence into n similarly-sized chunks.
10768+
// split_evenly(2, [0,1,2,3,4]) == [[0,1,2],[3,4]]
10769+
template <typename ContainerIn,
10770+
typename ContainerOut = std::vector<ContainerIn>>
10771+
ContainerOut split_evenly(std::size_t n, const ContainerIn& xs)
10772+
{
10773+
const std::size_t every_n = size_of_cont(xs) / n;
10774+
return split_at_idxs<
10775+
std::vector<std::size_t>,
10776+
ContainerIn,
10777+
ContainerOut>(
10778+
numbers_step<std::size_t>(
10779+
every_n, size_of_cont(xs), every_n),
10780+
xs);
10781+
}
10782+
1076510783
// API search type: split_by_token : ([a], Bool, [a]) -> [[a]]
1076610784
// fwd bind count: 2
1076710785
// Split a sequence at every segment matching a token.
@@ -15094,6 +15112,7 @@ fplus_curry_define_fn_2(insert_at_idx)
1509415112
fplus_curry_define_fn_1(partition)
1509515113
fplus_curry_define_fn_1(split_at_idxs)
1509615114
fplus_curry_define_fn_1(split_every)
15115+
fplus_curry_define_fn_1(split_evenly)
1509715116
fplus_curry_define_fn_2(split_by_token)
1509815117
fplus_curry_define_fn_1(run_length_encode_by)
1509915118
fplus_curry_define_fn_0(run_length_encode)
@@ -15702,6 +15721,7 @@ fplus_fwd_define_fn_2(insert_at_idx)
1570215721
fplus_fwd_define_fn_1(partition)
1570315722
fplus_fwd_define_fn_1(split_at_idxs)
1570415723
fplus_fwd_define_fn_1(split_every)
15724+
fplus_fwd_define_fn_1(split_evenly)
1570515725
fplus_fwd_define_fn_2(split_by_token)
1570615726
fplus_fwd_define_fn_1(run_length_encode_by)
1570715727
fplus_fwd_define_fn_0(run_length_encode)
@@ -16001,6 +16021,7 @@ fplus_fwd_flip_define_fn_1(split_at_idx)
1600116021
fplus_fwd_flip_define_fn_1(partition)
1600216022
fplus_fwd_flip_define_fn_1(split_at_idxs)
1600316023
fplus_fwd_flip_define_fn_1(split_every)
16024+
fplus_fwd_flip_define_fn_1(split_evenly)
1600416025
fplus_fwd_flip_define_fn_1(run_length_encode_by)
1600516026
fplus_fwd_flip_define_fn_1(span)
1600616027
fplus_fwd_flip_define_fn_1(aperture)

test/split_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ TEST_CASE("split_test - split functions")
9191
REQUIRE_EQ(split_at_idx(2, xs), std::make_pair(IntVector({ 1, 2 }), IntVector({ 2, 3, 2 })));
9292
REQUIRE_EQ(partition(is_even_int, xs), std::make_pair(IntVector({ 2, 2, 2 }), IntVector({ 1, 3 })));
9393
REQUIRE_EQ(split_every(2, xs), IntVectors({ { 1, 2 }, { 2, 3 }, { 2 } }));
94+
REQUIRE_EQ(split_evenly(1, xs), IntVectors({ { 1, 2, 2, 3, 2 } }));
95+
REQUIRE_EQ(split_evenly(2, xs), IntVectors({ { 1, 2, 2 }, { 3, 2 } }));
96+
REQUIRE_EQ(split_evenly(3, xs), IntVectors({ { 1, 2 }, { 2, 3 }, { 2 } }));
9497
auto splittedAt1And3 = split_at_idxs(IdxVector({ 1, 3 }), xs);
9598
IntVectors splittedAt1And3Dest = { IntVector({ 1 }), IntVector({ 2, 2 }), IntVector({ 3, 2 }) };
9699
REQUIRE_EQ(splittedAt1And3, splittedAt1And3Dest);

0 commit comments

Comments
 (0)