lol

lib.takeEnd: init (#398222)

authored by

Johannes Kirschbauer and committed by
GitHub
be393db7 246969a0

+98
+1
lib/default.nix
··· 279 279 naturalSort 280 280 compareLists 281 281 take 282 + takeEnd 282 283 drop 283 284 dropEnd 284 285 sublist
+34
lib/lists.nix
··· 1463 1463 take = count: sublist 0 count; 1464 1464 1465 1465 /** 1466 + Return the last (at most) N elements of a list. 1467 + 1468 + # Inputs 1469 + 1470 + `count` 1471 + 1472 + : Maximum number of elements to pick 1473 + 1474 + `list` 1475 + 1476 + : Input list 1477 + 1478 + # Type 1479 + 1480 + ``` 1481 + takeEnd :: int -> [a] -> [a] 1482 + ``` 1483 + 1484 + # Examples 1485 + :::{.example} 1486 + ## `lib.lists.takeEnd` usage example 1487 + 1488 + ```nix 1489 + takeEnd 2 [ "a" "b" "c" "d" ] 1490 + => [ "c" "d" ] 1491 + takeEnd 2 [ ] 1492 + => [ ] 1493 + ``` 1494 + 1495 + ::: 1496 + */ 1497 + takeEnd = n: xs: drop (max 0 (length xs - n)) xs; 1498 + 1499 + /** 1466 1500 Remove the first (at most) N elements of a list. 1467 1501 1468 1502 # Inputs
+63
lib/tests/misc.nix
··· 1357 1357 ) 1358 1358 ]; 1359 1359 1360 + testTakeEnd = 1361 + let 1362 + inherit (lib) takeEnd; 1363 + in 1364 + testAllTrue [ 1365 + ( 1366 + takeEnd 0 [ 1367 + 1 1368 + 2 1369 + 3 1370 + ] == [ ] 1371 + ) 1372 + ( 1373 + takeEnd 1 [ 1374 + 1 1375 + 2 1376 + 3 1377 + ] == [ 3 ] 1378 + ) 1379 + ( 1380 + takeEnd 2 [ 1381 + 1 1382 + 2 1383 + 3 1384 + ] == [ 1385 + 2 1386 + 3 1387 + ] 1388 + ) 1389 + ( 1390 + takeEnd 3 [ 1391 + 1 1392 + 2 1393 + 3 1394 + ] == [ 1395 + 1 1396 + 2 1397 + 3 1398 + ] 1399 + ) 1400 + ( 1401 + takeEnd 4 [ 1402 + 1 1403 + 2 1404 + 3 1405 + ] == [ 1406 + 1 1407 + 2 1408 + 3 1409 + ] 1410 + ) 1411 + (takeEnd 0 [ ] == [ ]) 1412 + (takeEnd 1 [ ] == [ ]) 1413 + ( 1414 + takeEnd (-1) [ 1415 + 1 1416 + 2 1417 + 3 1418 + ] == [ ] 1419 + ) 1420 + (takeEnd (-1) [ ] == [ ]) 1421 + ]; 1422 + 1360 1423 testDrop = 1361 1424 let 1362 1425 inherit (lib) drop;