-
Notifications
You must be signed in to change notification settings - Fork 3
Introduce purs-tidy formatter #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| !.gitignore | ||
| !.github | ||
| !.editorconfig | ||
| !.tidyrc.json | ||
|
|
||
| output | ||
| generated-docs | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "importSort": "source", | ||
| "importWrap": "source", | ||
| "indent": 2, | ||
| "operatorsFile": null, | ||
| "ribbon": 1, | ||
| "typeArrowPlacement": "first", | ||
| "unicode": "never", | ||
| "width": null | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| module Concurrent.BoundedQueue.Internal | ||
| ( BoundedQueue(..) | ||
| ) where | ||
| import Effect.AVar (AVar) | ||
| newtype BoundedQueue a = | ||
| BoundedQueue | ||
| { size ∷ Int | ||
| , contents ∷ Array (AVar a) | ||
| , readPos ∷ AVar Int | ||
| , writePos ∷ AVar Int | ||
| } | ||
| module Concurrent.BoundedQueue.Internal | ||
| ( BoundedQueue(..) | ||
| ) where | ||
|
|
||
| import Effect.AVar (AVar) | ||
|
|
||
| newtype BoundedQueue a = | ||
| BoundedQueue | ||
| { size :: Int | ||
| , contents :: Array (AVar a) | ||
| , readPos :: AVar Int | ||
| , writePos :: AVar Int | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,69 +1,68 @@ | ||
| module Concurrent.BoundedQueue.Sync | ||
| ( new | ||
| , isEmpty | ||
| , tryRead | ||
| , tryWrite | ||
| , module Export | ||
| ) | ||
| where | ||
|
|
||
| import Prelude | ||
|
|
||
| import Concurrent.BoundedQueue.Internal (BoundedQueue(..)) | ||
| import Concurrent.BoundedQueue.Internal (BoundedQueue) as Export | ||
| import Data.Array (unsafeIndex) | ||
| import Data.Maybe (Maybe(..)) | ||
| import Data.Unfoldable (replicateA) | ||
| import Effect (Effect) | ||
| import Effect.AVar as AVarEff | ||
| import Partial.Unsafe (unsafePartial) | ||
|
|
||
| -- | Synchronously creates a new `BoundedQueue` with the given capacity. | ||
| new ∷ ∀ a. Int → Effect (BoundedQueue a) | ||
| new size = do | ||
| contents ← replicateA size AVarEff.empty | ||
| readPos ← AVarEff.new 0 | ||
| writePos ← AVarEff.new 0 | ||
| pure (BoundedQueue { size, contents, readPos, writePos }) | ||
|
|
||
| -- | Synchronously checks whether the given queue is empty. Never blocks. | ||
| isEmpty ∷ ∀ a. BoundedQueue a → Effect Boolean | ||
| isEmpty (BoundedQueue q) = do | ||
| AVarEff.tryRead q.readPos >>= case _ of | ||
| Nothing → pure true | ||
| Just r → AVarEff.tryRead (unsafePartial unsafeIndex q.contents r) <#> | ||
| case _ of | ||
| Nothing → true | ||
| Just _ → false | ||
|
|
||
| -- | Synchronously attempts to read an element from the given queue. If the | ||
| -- | queue is empty, or there is a concurrent reader, returns `Nothing`. | ||
| tryRead ∷ ∀ a. BoundedQueue a -> Effect (Maybe a) | ||
| tryRead (BoundedQueue q) = do | ||
| mr ← AVarEff.tryTake q.readPos | ||
| case mr of | ||
| Just r → do | ||
| AVarEff.tryTake (unsafePartial unsafeIndex q.contents r) >>= case _ of | ||
| Just v → do | ||
| _ <- AVarEff.tryPut ((r + 1) `mod` q.size) q.readPos | ||
| pure (Just v) | ||
| Nothing → do | ||
| _ <- AVarEff.tryPut r q.readPos | ||
| pure Nothing | ||
| Nothing → pure Nothing | ||
|
|
||
| -- | Attempts to write an element into the given queue. If the queue is full, | ||
| -- | or there is a concurrent writer, returns `false` otherwise `true`. | ||
| tryWrite ∷ ∀ a. BoundedQueue a → a → Effect Boolean | ||
| tryWrite (BoundedQueue q) a = do | ||
| mw ← AVarEff.tryTake q.writePos | ||
| case mw of | ||
| Just w → do | ||
| AVarEff.tryPut a (unsafePartial unsafeIndex q.contents w) >>= if _ | ||
| then do | ||
| _ ← AVarEff.tryPut ((w + 1) `mod` q.size) q.writePos | ||
| pure true | ||
| else do | ||
| _ ← AVarEff.tryPut w q.writePos | ||
| pure false | ||
| Nothing → pure false | ||
| module Concurrent.BoundedQueue.Sync | ||
| ( new | ||
| , isEmpty | ||
| , tryRead | ||
| , tryWrite | ||
| , module Export | ||
| ) where | ||
|
|
||
| import Prelude | ||
|
|
||
| import Concurrent.BoundedQueue.Internal (BoundedQueue(..)) | ||
| import Concurrent.BoundedQueue.Internal (BoundedQueue) as Export | ||
| import Data.Array (unsafeIndex) | ||
| import Data.Maybe (Maybe(..)) | ||
| import Data.Unfoldable (replicateA) | ||
| import Effect (Effect) | ||
| import Effect.AVar as AVarEff | ||
| import Partial.Unsafe (unsafePartial) | ||
|
|
||
| -- | Synchronously creates a new `BoundedQueue` with the given capacity. | ||
| new :: forall a. Int -> Effect (BoundedQueue a) | ||
| new size = do | ||
| contents <- replicateA size AVarEff.empty | ||
| readPos <- AVarEff.new 0 | ||
| writePos <- AVarEff.new 0 | ||
| pure (BoundedQueue { size, contents, readPos, writePos }) | ||
|
|
||
| -- | Synchronously checks whether the given queue is empty. Never blocks. | ||
| isEmpty :: forall a. BoundedQueue a -> Effect Boolean | ||
| isEmpty (BoundedQueue q) = do | ||
| AVarEff.tryRead q.readPos >>= case _ of | ||
| Nothing -> pure true | ||
| Just r -> AVarEff.tryRead (unsafePartial unsafeIndex q.contents r) <#> | ||
| case _ of | ||
| Nothing -> true | ||
| Just _ -> false | ||
|
|
||
| -- | Synchronously attempts to read an element from the given queue. If the | ||
| -- | queue is empty, or there is a concurrent reader, returns `Nothing`. | ||
| tryRead :: forall a. BoundedQueue a -> Effect (Maybe a) | ||
| tryRead (BoundedQueue q) = do | ||
| mr <- AVarEff.tryTake q.readPos | ||
| case mr of | ||
| Just r -> do | ||
| AVarEff.tryTake (unsafePartial unsafeIndex q.contents r) >>= case _ of | ||
| Just v -> do | ||
| _ <- AVarEff.tryPut ((r + 1) `mod` q.size) q.readPos | ||
| pure (Just v) | ||
| Nothing -> do | ||
| _ <- AVarEff.tryPut r q.readPos | ||
| pure Nothing | ||
| Nothing -> pure Nothing | ||
|
|
||
| -- | Attempts to write an element into the given queue. If the queue is full, | ||
| -- | or there is a concurrent writer, returns `false` otherwise `true`. | ||
| tryWrite :: forall a. BoundedQueue a -> a -> Effect Boolean | ||
| tryWrite (BoundedQueue q) a = do | ||
| mw <- AVarEff.tryTake q.writePos | ||
| case mw of | ||
| Just w -> do | ||
| AVarEff.tryPut a (unsafePartial unsafeIndex q.contents w) >>= | ||
| if _ then do | ||
| _ <- AVarEff.tryPut ((w + 1) `mod` q.size) q.writePos | ||
| pure true | ||
| else do | ||
| _ <- AVarEff.tryPut w q.writePos | ||
| pure false | ||
| Nothing -> pure false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on this style?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm personally fine with it!