1 package Data
::SExpression
::Util
;
6 use parent qw
/Exporter/;
8 our %EXPORT_TAGS = ( 'all' => [
16 our @EXPORT_OK = ( @
{ $EXPORT_TAGS{'all'} } );
18 our $VERSION = '0.000_001';
20 use Data
::SExpression
::Cons
;
24 Data
::SExpression
::Cons
->new($car, $cdr);
28 my ($expr, $rest) = @_;
30 cons
$expr->car, append
($expr->cdr, $rest)
39 my ($block, $expr) = @_;
43 local $_ = $expr->car;
46 cons
$result, mapcar
{ $block->($_) } $expr->cdr
53 my ($expr, $acc) = @_;
55 revacc
($expr->cdr, cons
($expr->car, $acc))
67 my ($expr, $list, $acc) = @_;
70 } elsif ($list->car eq $expr) {
73 positionacc
($expr, $list->cdr, $acc + 1)
78 my ($expr, $list) = @_;
79 positionacc
$expr, $list, 0
89 Data::SExpression::Util - routines for processing linked lists
93 use Data::SExpression::Util qw/:all/;
94 my $list = cons 1, cons 2, cons 3, undef; # (1 2 3)
95 my $other_list = cons 4, cons 5, undef; # (4 5)
97 $list = append $list, $other_list; # $list is now (1 2 3 4 5)
99 say position 1, $list; # 0
100 say position 4, $list; # 3
101 say 'undef' unless defined position 0, $list; # undef
103 $list = rev $list; # (5 4 3 2 1)
104 $list = mapcar { $_ + 1 } $list; # (6 5 4 3 2)
106 say position 2, $list; # 4
110 Data::SExpression::Util contains several routines for processing
111 linked lists (represented L<Data::SExpression::Cons> objects). These
112 are analogous to Lisp functions with the same names.
114 Right now very few functions are implemented, more will come in the
117 The list of functions is:
121 =item B<append>(I<$list>, I<$other_list>)
123 Appends the list I<$other_list> at the end of the list I<$list>.
125 =item B<mapcar> { I<block> } I<$list>
127 Analogous to Perl's map function. Runs I<block> with each element of
128 the list I<$list> as $_, and then returns a containing all of the
131 =item B<rev>(I<$list>)
135 =item B<position>(I<$elt>, I<$list>)
137 Searches for I<$elt> in I<$list> and returns the first matching
138 element (comparison is done via eq).
144 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
146 =head1 COPYRIGHT AND LICENSE
148 Copyright (C) 2018 by Marius Gavrilescu
150 This library is free software; you can redistribute it and/or modify
151 it under the same terms as Perl itself, either Perl version 5.24.1 or,
152 at your option, any later version of Perl 5 you may have available.
This page took 0.026637 seconds and 4 git commands to generate.