Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
iter.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
14
15#include "value.h"
16#include <stdlib.h>
17
31 int n = array_length(arr);
32 if (n <= 0) return make_array_from_values(NULL, 0);
33 Value *pairs = (Value *)malloc(sizeof(Value) * n);
34 if (!pairs) return make_array_from_values(NULL, 0);
35 for (int i = 0; i < n; ++i) {
36 Value elem;
37 array_get_copy(arr, i, &elem);
38 Value kv_vals[2];
39 kv_vals[0] = make_int(i);
40 kv_vals[1] = elem;
41 Value kv = make_array_from_values(kv_vals, 2);
42 free_value(kv_vals[0]);
43 free_value(kv_vals[1]);
44 pairs[i] = kv;
45 }
47 for (int i = 0; i < n; ++i)
48 free_value(pairs[i]);
49 free(pairs);
50 return out;
51}
52
66Value bi_zip(const Value *a, const Value *b) {
67 int na = array_length(a);
68 int nb = array_length(b);
69 int n = na < nb ? na : nb;
70 if (n <= 0) return make_array_from_values(NULL, 0);
71 Value *pairs = (Value *)malloc(sizeof(Value) * n);
72 if (!pairs) return make_array_from_values(NULL, 0);
73 for (int i = 0; i < n; ++i) {
74 Value av, bv;
75 array_get_copy(a, i, &av);
76 array_get_copy(b, i, &bv);
77 Value kv_vals[2];
78 kv_vals[0] = av;
79 kv_vals[1] = bv;
80 Value kv = make_array_from_values(kv_vals, 2);
81 free_value(kv_vals[0]);
82 free_value(kv_vals[1]);
83 pairs[i] = kv;
84 }
86 for (int i = 0; i < n; ++i)
87 free_value(pairs[i]);
88 free(pairs);
89 return out;
90}
Value a
Definition add.c:37
Value out
Definition apop.c:38
uint32_t b
Definition band.c:32
array_clear & arr
Definition clear.c:38
int n
Definition insert.c:41
Value bi_zip(const Value *a, const Value *b)
Zip two arrays into an array of pairs up to the shorter length.
Definition iter.c:66
Value bi_enumerate(const Value *arr)
Build an array of [index, value] pairs from an input array.
Definition iter.c:30
free(vals)
Tagged union representing a Fun value.
Definition value.h:68
int array_length(const Value *v)
Get the element count of an array Value.
Definition value.c:176
void free_value(Value v)
Free dynamic storage owned by a Value.
Definition value.c:517
int array_get_copy(const Value *v, int index, Value *out)
Copy an array element into out.
Definition value.c:192
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
Definition value.c:142
Defines the Value type and associated functions for the Fun VM.