Haskell mapM_ to other format
Although I have the feeling I am progressing in Haskell I am still not a hundred percent comfortable with contexts. Take the code here:
extractData :: IO ()
extractData = do
id <- getLine
let userToolIDSelect = (read id) :: Int
connection <- open "tools.db"
resp <- query connection "SELECT * FROM tools WHERE toolID = (?);"
(Only userToolIDSelect) :: IO [Tool]
mapM_ print resp
Works fine, but how I can use mapM_
to generate something I can work with? I can only get it to print to the console, but I would like to have eg. a list back so I can write it to a file and import it in another file for processing...
The number of possibilities and libraries in Haskell dazzles me a bit and makes me loose focus sometimes. Think this is that time again..
haskell io
add a comment |
Although I have the feeling I am progressing in Haskell I am still not a hundred percent comfortable with contexts. Take the code here:
extractData :: IO ()
extractData = do
id <- getLine
let userToolIDSelect = (read id) :: Int
connection <- open "tools.db"
resp <- query connection "SELECT * FROM tools WHERE toolID = (?);"
(Only userToolIDSelect) :: IO [Tool]
mapM_ print resp
Works fine, but how I can use mapM_
to generate something I can work with? I can only get it to print to the console, but I would like to have eg. a list back so I can write it to a file and import it in another file for processing...
The number of possibilities and libraries in Haskell dazzles me a bit and makes me loose focus sometimes. Think this is that time again..
haskell io
This is too vague. You want to replacemapM_
with "something I can work with", which could be anything at all. You should clarify your goal. Note that you already have a list:resp
. You can work with that withoutmapM_
, if you want. E.g. you can writeresp
to a file.
– chi
Nov 15 '18 at 12:48
If you need to work with mutable structure, e.g. STArray, you may find that the functions like mapM_, forM_ or etc are useful.
– assembly.jc
Nov 15 '18 at 14:01
add a comment |
Although I have the feeling I am progressing in Haskell I am still not a hundred percent comfortable with contexts. Take the code here:
extractData :: IO ()
extractData = do
id <- getLine
let userToolIDSelect = (read id) :: Int
connection <- open "tools.db"
resp <- query connection "SELECT * FROM tools WHERE toolID = (?);"
(Only userToolIDSelect) :: IO [Tool]
mapM_ print resp
Works fine, but how I can use mapM_
to generate something I can work with? I can only get it to print to the console, but I would like to have eg. a list back so I can write it to a file and import it in another file for processing...
The number of possibilities and libraries in Haskell dazzles me a bit and makes me loose focus sometimes. Think this is that time again..
haskell io
Although I have the feeling I am progressing in Haskell I am still not a hundred percent comfortable with contexts. Take the code here:
extractData :: IO ()
extractData = do
id <- getLine
let userToolIDSelect = (read id) :: Int
connection <- open "tools.db"
resp <- query connection "SELECT * FROM tools WHERE toolID = (?);"
(Only userToolIDSelect) :: IO [Tool]
mapM_ print resp
Works fine, but how I can use mapM_
to generate something I can work with? I can only get it to print to the console, but I would like to have eg. a list back so I can write it to a file and import it in another file for processing...
The number of possibilities and libraries in Haskell dazzles me a bit and makes me loose focus sometimes. Think this is that time again..
haskell io
haskell io
edited Nov 15 '18 at 13:39
duplode
23k44987
23k44987
asked Nov 15 '18 at 12:17
MadderoteMadderote
271111
271111
This is too vague. You want to replacemapM_
with "something I can work with", which could be anything at all. You should clarify your goal. Note that you already have a list:resp
. You can work with that withoutmapM_
, if you want. E.g. you can writeresp
to a file.
– chi
Nov 15 '18 at 12:48
If you need to work with mutable structure, e.g. STArray, you may find that the functions like mapM_, forM_ or etc are useful.
– assembly.jc
Nov 15 '18 at 14:01
add a comment |
This is too vague. You want to replacemapM_
with "something I can work with", which could be anything at all. You should clarify your goal. Note that you already have a list:resp
. You can work with that withoutmapM_
, if you want. E.g. you can writeresp
to a file.
– chi
Nov 15 '18 at 12:48
If you need to work with mutable structure, e.g. STArray, you may find that the functions like mapM_, forM_ or etc are useful.
– assembly.jc
Nov 15 '18 at 14:01
This is too vague. You want to replace
mapM_
with "something I can work with", which could be anything at all. You should clarify your goal. Note that you already have a list: resp
. You can work with that without mapM_
, if you want. E.g. you can write resp
to a file.– chi
Nov 15 '18 at 12:48
This is too vague. You want to replace
mapM_
with "something I can work with", which could be anything at all. You should clarify your goal. Note that you already have a list: resp
. You can work with that without mapM_
, if you want. E.g. you can write resp
to a file.– chi
Nov 15 '18 at 12:48
If you need to work with mutable structure, e.g. STArray, you may find that the functions like mapM_, forM_ or etc are useful.
– assembly.jc
Nov 15 '18 at 14:01
If you need to work with mutable structure, e.g. STArray, you may find that the functions like mapM_, forM_ or etc are useful.
– assembly.jc
Nov 15 '18 at 14:01
add a comment |
1 Answer
1
active
oldest
votes
Well, you just pass a function you want instead of print
. Either using lambda, for simple things:
mapM_ (tool -> ...) resp
or as separate IO action:
doSomethingWithTool :: Tool -> IO ()
doSomethingWithTool tool = do
...
return ()
and then
mapM_ doSomethingWithTool resp
@chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.
– Madderote
Nov 15 '18 at 15:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319338%2fhaskell-mapm-to-other-format%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Well, you just pass a function you want instead of print
. Either using lambda, for simple things:
mapM_ (tool -> ...) resp
or as separate IO action:
doSomethingWithTool :: Tool -> IO ()
doSomethingWithTool tool = do
...
return ()
and then
mapM_ doSomethingWithTool resp
@chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.
– Madderote
Nov 15 '18 at 15:59
add a comment |
Well, you just pass a function you want instead of print
. Either using lambda, for simple things:
mapM_ (tool -> ...) resp
or as separate IO action:
doSomethingWithTool :: Tool -> IO ()
doSomethingWithTool tool = do
...
return ()
and then
mapM_ doSomethingWithTool resp
@chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.
– Madderote
Nov 15 '18 at 15:59
add a comment |
Well, you just pass a function you want instead of print
. Either using lambda, for simple things:
mapM_ (tool -> ...) resp
or as separate IO action:
doSomethingWithTool :: Tool -> IO ()
doSomethingWithTool tool = do
...
return ()
and then
mapM_ doSomethingWithTool resp
Well, you just pass a function you want instead of print
. Either using lambda, for simple things:
mapM_ (tool -> ...) resp
or as separate IO action:
doSomethingWithTool :: Tool -> IO ()
doSomethingWithTool tool = do
...
return ()
and then
mapM_ doSomethingWithTool resp
answered Nov 15 '18 at 12:47
arrowdarrowd
22.8k45483
22.8k45483
@chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.
– Madderote
Nov 15 '18 at 15:59
add a comment |
@chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.
– Madderote
Nov 15 '18 at 15:59
@chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.
– Madderote
Nov 15 '18 at 15:59
@chi: it is vague. Sorry. [at]Arrowd understood. That was the answer I was looking for. Was trying to get it out of the Monad, but then realized that that is impossible.
– Madderote
Nov 15 '18 at 15:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319338%2fhaskell-mapm-to-other-format%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This is too vague. You want to replace
mapM_
with "something I can work with", which could be anything at all. You should clarify your goal. Note that you already have a list:resp
. You can work with that withoutmapM_
, if you want. E.g. you can writeresp
to a file.– chi
Nov 15 '18 at 12:48
If you need to work with mutable structure, e.g. STArray, you may find that the functions like mapM_, forM_ or etc are useful.
– assembly.jc
Nov 15 '18 at 14:01