|
15 | 15 | package grumpy
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "bytes" |
18 | 19 | "fmt"
|
19 | 20 | "math"
|
20 | 21 | "math/big"
|
@@ -248,6 +249,82 @@ func builtinMapFn(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
|
248 | 249 | return NewList(result...).ToObject(), nil
|
249 | 250 | }
|
250 | 251 |
|
| 252 | +func builtinFilter(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) { |
| 253 | + argc := len(args) |
| 254 | + if argc < 2 { |
| 255 | + return nil, f.RaiseType(TypeErrorType, fmt.Sprintf("filter expected 2 arguments, got %d", argc)) |
| 256 | + } |
| 257 | + fn := args[0] |
| 258 | + l := args[1] |
| 259 | + switch { |
| 260 | + case l.isInstance(TupleType): |
| 261 | + result := make([]*Object, 0) |
| 262 | + for _, item := range toTupleUnsafe(l).elems { |
| 263 | + if fn == None { |
| 264 | + if ret, raised := IsTrue(f, item); raised != nil { |
| 265 | + return nil, raised |
| 266 | + } else if ret { |
| 267 | + result = append(result, item) |
| 268 | + } |
| 269 | + } else { |
| 270 | + ret, raised := args[0].Call(f, []*Object{item}, nil) |
| 271 | + if raised != nil { |
| 272 | + return nil, raised |
| 273 | + } |
| 274 | + if ret, raised := IsTrue(f, ret); raised != nil { |
| 275 | + return nil, raised |
| 276 | + } else if ret { |
| 277 | + result = append(result, item) |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + return NewTuple(result...).ToObject(), nil |
| 282 | + case l.isInstance(StrType): |
| 283 | + if fn == None { |
| 284 | + return l, nil |
| 285 | + } |
| 286 | + var result bytes.Buffer |
| 287 | + for _, item := range toStrUnsafe(l).Value() { |
| 288 | + ret, raised := fn.Call(f, []*Object{NewStr(string(item)).ToObject()}, nil) |
| 289 | + if raised != nil { |
| 290 | + return nil, raised |
| 291 | + } |
| 292 | + if ret, raised := IsTrue(f, ret); raised != nil { |
| 293 | + return nil, raised |
| 294 | + } else if ret { |
| 295 | + result.WriteRune(item) |
| 296 | + } |
| 297 | + } |
| 298 | + return NewStr(result.String()).ToObject(), nil |
| 299 | + default: |
| 300 | + result := make([]*Object, 0) |
| 301 | + raised := seqForEach(f, l, func(item *Object) (raised *BaseException) { |
| 302 | + if fn == None { |
| 303 | + if ret, raised := IsTrue(f, item); raised != nil { |
| 304 | + return raised |
| 305 | + } else if ret { |
| 306 | + result = append(result, item) |
| 307 | + } |
| 308 | + } else { |
| 309 | + ret, raised := fn.Call(f, []*Object{item}, nil) |
| 310 | + if raised != nil { |
| 311 | + return raised |
| 312 | + } |
| 313 | + if ret, raised := IsTrue(f, ret); raised != nil { |
| 314 | + return raised |
| 315 | + } else if ret { |
| 316 | + result = append(result, item) |
| 317 | + } |
| 318 | + } |
| 319 | + return nil |
| 320 | + }) |
| 321 | + if raised != nil { |
| 322 | + return nil, raised |
| 323 | + } |
| 324 | + return NewList(result...).ToObject(), nil |
| 325 | + } |
| 326 | +} |
| 327 | + |
251 | 328 | func builtinAll(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
|
252 | 329 | if raised := checkFunctionArgs(f, "all", args, ObjectType); raised != nil {
|
253 | 330 | return nil, raised
|
@@ -768,6 +845,7 @@ func init() {
|
768 | 845 | "divmod": newBuiltinFunction("divmod", builtinDivMod).ToObject(),
|
769 | 846 | "Ellipsis": Ellipsis,
|
770 | 847 | "False": False.ToObject(),
|
| 848 | + "filter": newBuiltinFunction("filter", builtinFilter).ToObject(), |
771 | 849 | "getattr": newBuiltinFunction("getattr", builtinGetAttr).ToObject(),
|
772 | 850 | "globals": newBuiltinFunction("globals", builtinGlobals).ToObject(),
|
773 | 851 | "hasattr": newBuiltinFunction("hasattr", builtinHasAttr).ToObject(),
|
|
0 commit comments