├── .gitignore ├── .editorconfig ├── README.md ├── elm.json ├── LICENSE └── src └── List └── Split.elm /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore build or dist files 2 | /elm-stuff 3 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | [*.elm] 2 | indent_style = space 3 | indent_size = 4 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Split lists into chunks 2 | Experimental package with convenient string splitting functions. 3 | Note that this API is experimental and likely to go through many more iterations. 4 | 5 | This package is partly inspired by the haskell package [split]. 6 | 7 | Feedback and contributions are very welcome. 8 | 9 | [split]: http://hackage.haskell.org/package/split 10 | -------------------------------------------------------------------------------- /elm.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "package", 3 | "name": "elm-community/list-split", 4 | "summary": "Split lists into chunks", 5 | "license": "MIT", 6 | "version": "1.0.3", 7 | "exposed-modules": [ 8 | "List.Split" 9 | ], 10 | "elm-version": "0.19.0 <= v < 0.20.0", 11 | "dependencies": { 12 | "elm/core": "1.0.0 <= v < 2.0.0" 13 | }, 14 | "test-dependencies": {} 15 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 CircuitHub, Elm Community Members 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/List/Split.elm: -------------------------------------------------------------------------------- 1 | module List.Split exposing (chunksOfLeft, chunksOfRight) 2 | 3 | {-| Split lists into chunks 4 | 5 | 6 | # Splitters 7 | 8 | @docs chunksOfLeft, chunksOfRight 9 | 10 | -} 11 | 12 | import List exposing (..) 13 | 14 | 15 | {-| Split list into smaller lists of length `k`, starting from the left. 16 | 17 | chunksOfLeft 3 (List.range 1 9) == [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 18 | chunksOfLeft 3 [1,2,3,4,5,6,7,8] == [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8 ] ] 19 | chunksOfLeft 3 [] == [[]] 20 | chunksOfLeft 0 xs == [[]] 21 | chunksOfLeft -1 xs == [] 22 | 23 | -} 24 | chunksOfLeft : Int -> List a -> List (List a) 25 | chunksOfLeft k xs = 26 | if k == 0 then 27 | [ [] ] 28 | else if k < 0 then 29 | [] 30 | else if length xs > k then 31 | take k xs :: chunksOfLeft k (drop k xs) 32 | else 33 | [ xs ] 34 | 35 | 36 | {-| Split list into smaller lists of length `k`, starting from the right. 37 | 38 | chunksOfRight 3 (List.range 1 9) == [ [ 7, 8, 9 ], [ 4, 5, 6 ], [ 1, 2, 3 ] ] 39 | chunksOfRight 3 [1,2,3,4,5,6,7,8] == [ [ 6, 7, 8 ], [ 3, 4, 5 ], [ 1, 2 ] ] 40 | chunksOfRight 3 [] == [[]] 41 | chunksOfRight 0 xs == [[]] 42 | chunksOfRight -1 xs == [] 43 | 44 | -} 45 | chunksOfRight : Int -> List a -> List (List a) 46 | chunksOfRight k = 47 | map reverse << chunksOfLeft k << reverse 48 | --------------------------------------------------------------------------------