Skip to content

Commit 0a69e6d

Browse files
aiya000t2013anurag
authored andcommitted
Create 'dequeue in Haskell' (hacktoberfest17#1659)
* Create 'dequeue in Haskell' * Add a contributor
1 parent 4a32fe4 commit 0a69e6d

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,4 +1065,4 @@ Programming contributors (sorted contributions)
10651065

10661066
* **[kserno](https://github.com/kserno)**
10671067

1068-
1068+
* **[aiya000](https://github.com/aiya000)**

dequeue/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.stack-work

dequeue/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

dequeue/dequeue.cabal

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: dequeue
2+
version: 0.1.0.0
3+
category: Hacktoberfest17
4+
build-type: Simple
5+
cabal-version: >=1.10
6+
7+
executable dequeue
8+
hs-source-dirs: .
9+
main-is: dequeue.hs
10+
ghc-options: -threaded -rtsopts -with-rtsopts=-N
11+
build-depends: base
12+
, containers
13+
, lens
14+
, mtl
15+
default-language: Haskell2010

dequeue/dequeue.hs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Control.Lens hiding ((<|), (|>), (:<), (:>))
2+
import Control.Monad (void)
3+
import Control.Monad.IO.Class (liftIO)
4+
import Control.Monad.State.Lazy (StateT(..), runStateT, get, gets)
5+
import Data.Sequence
6+
7+
-- 'Seq' is a dequeue
8+
type Dequeue a = Seq a
9+
10+
main :: IO ()
11+
main = do
12+
putStrLn "-- pure operations"
13+
let xs = fromList [10]
14+
xs' = 20 <| xs -- push front
15+
xs'' = xs' |> 30 -- push back
16+
mapM_ print [xs, xs', xs'']
17+
--NOTE: these are unsafe operations (Please see 'context' for safe operations)
18+
let (x' :< xs') = viewl xs''
19+
(xs :> x) = viewr xs'
20+
mapM_ print [x', x]
21+
void $ runStateT context xs
22+
23+
context :: StateT (Dequeue Int) IO ()
24+
context = do
25+
liftIO $ putStrLn "-- inpure operations"
26+
printState
27+
simple %= (20 <|)
28+
printState
29+
simple %= (|> 30)
30+
printState
31+
--NOTE: these are safe operations by 'Prism'
32+
maybeHead <- gets $ preview _head
33+
liftIO $ print maybeHead
34+
maybeLast <- gets $ preview _last
35+
liftIO $ print maybeLast
36+
where
37+
printState :: Show s => StateT s IO ()
38+
printState = get >>= liftIO . print

dequeue/stack.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# This file was automatically generated by 'stack init'
2+
#
3+
# Some commonly used options have been documented as comments in this file.
4+
# For advanced use and comprehensive documentation of the format, please see:
5+
# https://docs.haskellstack.org/en/stable/yaml_configuration/
6+
7+
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
8+
# A snapshot resolver dictates the compiler version and the set of packages
9+
# to be used for project dependencies. For example:
10+
#
11+
# resolver: lts-3.5
12+
# resolver: nightly-2015-09-21
13+
# resolver: ghc-7.10.2
14+
# resolver: ghcjs-0.1.0_ghc-7.10.2
15+
# resolver:
16+
# name: custom-snapshot
17+
# location: "./custom-snapshot.yaml"
18+
resolver: lts-9.10
19+
20+
# User packages to be built.
21+
# Various formats can be used as shown in the example below.
22+
#
23+
# packages:
24+
# - some-directory
25+
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
26+
# - location:
27+
# git: https://github.com/commercialhaskell/stack.git
28+
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
29+
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
30+
# extra-dep: true
31+
# subdirs:
32+
# - auto-update
33+
# - wai
34+
#
35+
# A package marked 'extra-dep: true' will only be built if demanded by a
36+
# non-dependency (i.e. a user package), and its test suites and benchmarks
37+
# will not be run. This is useful for tweaking upstream packages.
38+
packages:
39+
- .
40+
# Dependency packages to be pulled from upstream that are not in the resolver
41+
# (e.g., acme-missiles-0.3)
42+
# extra-deps: []
43+
44+
# Override default flag values for local packages and extra-deps
45+
# flags: {}
46+
47+
# Extra package databases containing global packages
48+
# extra-package-dbs: []
49+
50+
# Control whether we use the GHC we find on the path
51+
# system-ghc: true
52+
#
53+
# Require a specific version of stack, using version ranges
54+
# require-stack-version: -any # Default
55+
# require-stack-version: ">=1.5"
56+
#
57+
# Override the architecture used by stack, especially useful on Windows
58+
# arch: i386
59+
# arch: x86_64
60+
#
61+
# Extra directories used by stack for building
62+
# extra-include-dirs: [/path/to/dir]
63+
# extra-lib-dirs: [/path/to/dir]
64+
#
65+
# Allow a newer minor version of GHC than the snapshot specifies
66+
# compiler-check: newer-minor

0 commit comments

Comments
 (0)