Skip to content

Commit 5c09f91

Browse files
committed
Add apWhen
1 parent 90a368b commit 5c09f91

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

clash-prelude/src/Clash/Explicit/Signal.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Copyright : (C) 2013-2016, University of Twente,
33
2016-2019, Myrtle Software,
44
2017-2022, Google Inc.
55
2020 , Ben Gamari,
6-
2021-2024, QBayLogic B.V.
6+
2021-2025, QBayLogic B.V.
77
License : BSD2 (see the file LICENSE)
88
Maintainer : QBayLogic B.V. <devops@qbaylogic.com>
99
@@ -238,6 +238,7 @@ module Clash.Explicit.Signal
238238
, regMaybe
239239
, regEn
240240
, mux
241+
, apWhen
241242
-- * Simulation and testbench functions
242243
, clockGen
243244
, resetGen

clash-prelude/src/Clash/Signal.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Copyright : (C) 2013-2016, University of Twente,
33
2016-2019, Myrtle Software Ltd,
44
2017 , Google Inc.,
5-
2021-2024, QBayLogic B.V.
5+
2021-2025, QBayLogic B.V.
66
License : BSD2 (see the file LICENSE)
77
Maintainer : QBayLogic B.V. <devops@qbaylogic.com>
88
@@ -202,6 +202,7 @@ module Clash.Signal
202202
, regMaybe
203203
, regEn
204204
, mux
205+
, apWhen
205206
-- * Simulation and testbench functions
206207
, clockGen
207208
, resetGen

clash-prelude/src/Clash/Signal/Internal.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ module Clash.Signal.Internal
126126
, syncRegister#
127127
, registerPowerup#
128128
, mux
129+
, apWhen
129130
-- * Simulation and testbench functions
130131
, clockGen
131132
, tbClockGen
@@ -1653,6 +1654,13 @@ mux :: Applicative f => f Bool -> f a -> f a -> f a
16531654
mux = liftA3 (\b t f -> if b then t else f)
16541655
{-# INLINE mux #-}
16551656

1657+
-- | Updates a value inside an 'Applicative' context if and only if
1658+
-- the given Boolean condition evaluates to 'True' in the same
1659+
-- context.
1660+
apWhen :: Applicative f => f Bool -> (a -> a) -> f a -> f a
1661+
apWhen cond upd x = mux cond (upd <$> x) x
1662+
{-# INLINE apWhen #-}
1663+
16561664
infix 4 .==.
16571665
-- | The above type is a generalization for:
16581666
--

0 commit comments

Comments
 (0)