stdlib: Implement last() and nth() functions.
[goals.git] / stdlib / prelude.gl
1 # Goals stdlib prelude.
2 # Copyright (C) 2019 Richard W.M. Jones
3 # Copyright (C) 2019 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 # This file is included first and automatically in all Goalfiles
20 # (unless you use --no-prelude).  It contains standard functions and
21 # tactics.
22
23 # The only tactic that ‘make’ has.
24 tactic *file (filename) = @{
25     # Rebuild if the target file doesn't exist at all.
26     test -f %filename || exit 99
27
28     # Otherwise rebuild if it is older than any dependency.
29     for f in %<; do
30         test %filename -ot "$f" && exit 99 ||:
31     done
32 }
33
34 # This is a simpler tactic than the above since it will
35 # rebuild if the file is missing, but not if it is older.
36 tactic *exists (filename) = @{
37     test -f %filename || exit 99
38 }
39
40 #----------------------------------------------------------------------
41 # Basic functions.
42
43 function error (msg) = @{
44     echo %msg >&2
45     exit 1
46 }
47
48 # Wrap list of strings in a call or tactic.
49 pure function wrap (wrapper, xs) = @{
50     echo '['
51     for x in %xs; do
52         echo %wrapper "( "
53         quoted_string "$x"
54         echo " ),"
55     done
56     echo ']'
57 }
58
59 #----------------------------------------------------------------------
60 # Text functions.
61
62 # Filter a list by regexp.
63 pure function filter (pat, xs) returning strings = @{
64     for f in %xs; do echo "$f"; done | grep -E -- %pat
65 }
66
67 # Filter out a list by regexp.
68 pure function filter-out (pat, xs) returning strings = @{
69     for f in %xs; do echo "$f"; done | grep -v -E -- %pat
70 }
71
72 # Head of a list.
73 pure function head (xs) returning string = @{
74     for f in %xs; do
75         echo "$f"
76         exit 0
77     done
78 }
79
80 # Join two lists.
81 pure function join (xs, ys) returning strings = @{
82     for f in %xs %ys; do echo "$f"; done
83 }
84
85 # Last element of a list.
86 pure function last (xs) returning string = @{
87     for f in %xs; do
88         r="$f"
89     done
90     echo "$r"
91 }
92
93 # n'th element of a list.
94 pure function nth (n, xs) returning string = @{
95     i=0
96     r=
97     for f in %xs; do
98         if [ $i -eq %n ]; then r="$f"; fi
99         ((++i))
100     done
101     echo "$r"
102 }
103
104 # Sort + uniq a list.
105 pure function sort (xs) returning strings = @{
106     for f in %xs; do echo "$f"; done | sort -u
107 }
108
109 # Substitute.
110 pure function subst (from, to, text) returning string = @{
111     # We need to replace any / characters in ‘to’ with escaped ones.
112     to="$( echo -n %to | sed 's,/,\\/,g' )"
113     echo %text | sed -E s/%from/$to/g
114 }
115
116 # Tail of a list.
117 pure function tail (xs) returning strings = @{
118     drop=1
119     for f in %xs; do
120         if [ -z "$drop" ]; then echo "$f"; fi
121         drop=
122     done
123 }
124
125 #----------------------------------------------------------------------
126 # File functions.
127
128 # Base name.
129 pure function basename (name) returning string = @{
130     basename -- %name
131 }
132
133 # Directory name.
134 pure function dirname (name) returning string = @{
135     dirname -- %name
136 }
137
138 # File extension.
139 pure function extension (name) returning string = @{
140     name=%name
141     echo "${name##*.}"
142 }
143
144 # Read a file.
145 function read (filename) returning string = @{
146     cat -- %filename
147 }
148
149 # Read a file as a list of lines.
150 function readlines (filename) returning strings = @{
151     cat -- %filename
152 }
153
154 # Real path.
155 function realpath (filename) returning string = @{
156     realpath -- %filename
157 }
158
159 # Expand a wildcard into a list of filenames.
160 #
161 # This function is probably not "pure" since it depends on the
162 # current working directory and also files may be created in
163 # the directory while goals is running which would affect the
164 # result.
165 function wildcard (wc) returning strings = @{
166     shopt -s nullglob
167     # Note that the substitution is quoted by goals, so to expand
168     # it we must assign it to a variable and then use it unquoted.
169     wc=%wc
170     for f in $wc; do echo "$f"; done
171 }