* input data for babel blocks
@ 2013-09-30 12:50 Alan Schmitt
2013-09-30 16:26 ` Charles Berry
2013-10-01 0:04 ` Eric Schulte
0 siblings, 2 replies; 13+ messages in thread
From: Alan Schmitt @ 2013-09-30 12:50 UTC (permalink / raw)
To: emacs-org list
Hello,
In my quest for analyzing my data in org mode tables, I'm trying to see
if I can use my favorite language (i.e., ocaml). I'm thus looking at how
to input such tables in a caml program. I found that the following works
well:
--8<---------------cut here---------------start------------->8---
#+name: data
| 12 |
| 42 |
#+BEGIN_SRC ocaml :var x=data
x
#+END_SRC
#+RESULTS:
: - : int array array = [|[|12|]; [|42|]|]
--8<---------------cut here---------------end--------------->8---
(The only "bad" thing is that a single value is an array, but this is
probably related to the following thing.) Unfortunately (for interaction
with org mode), caml is strongly typed, and arrays must be
homogeneous. Thus the following fails:
--8<---------------cut here---------------start------------->8---
#+name: myinput
| x | 12 |
| y | 24 |
#+BEGIN_SRC ocaml :var x=myinput
x
#+END_SRC
--8<---------------cut here---------------end--------------->8---
with the error in the toplevel:
--8<---------------cut here---------------start------------->8---
# let x = [|[|"x"; 12|]; [|"y"; 24|]|];;
x;;
"org-babel-ocaml-eoe";;
Characters 17-19:
let x = [|[|"x"; 12|]; [|"y"; 24|]|];;
^^
Error: This expression has type int but an expression was expected of type
string
--8<---------------cut here---------------end--------------->8---
I would like to change the parsing of table in ob-ocaml so that it
generates an array of _tuples_, for instance for the previous example:
#+BEGIN_SRC ocaml
let x = [| ("x", 12); ("y", 24) |];;
#+END_SRC
I looked at the code, and this seems to be the relevant part:
#+BEGIN_SRC emacs-lisp
(defun org-babel-variable-assignments:ocaml (params)
"Return list of ocaml statements assigning the block's variables."
(mapcar
(lambda (pair) (format "let %s = %s;;" (car pair)
(org-babel-ocaml-elisp-to-ocaml (cdr pair))))
(mapcar #'cdr (org-babel-get-header params :var))))
(defun org-babel-ocaml-elisp-to-ocaml (val)
"Return a string of ocaml code which evaluates to VAL."
(if (listp val)
(concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml val "; ") "|]")
(format "%S" val)))
#+END_SRC
I tried tweaking this to the following:
#+BEGIN_SRC emacs-lisp
(defun org-babel-variable-assignments:ocaml (params)
"Return list of ocaml statements assigning the block's variables."
(mapcar
(lambda (pair) (format "let %s = %s;;" (car pair)
(org-babel-ocaml-elisp-to-ocaml (cdr pair))))
(mapcar #'cdr (org-babel-get-header params :var))))
(defun org-babel-ocaml-elisp-to-ocaml (val)
"Return a string of ocaml code which evaluates to VAL."
(if (listp val)
(concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml-tuple val "; ") "|]")
(format "%S" val)))
(defun org-babel-ocaml-elisp-to-ocaml-tuple (val)
"Return a string of ocaml code which evaluates to VAL, as a tuple."
(if (listp val)
(concat "(" (mapconcat #'org-babel-ocaml-elisp-to-ocaml-tuple val ", ") ")")
(format "%S" val)))
#+END_SRC
Now the example seems to work:
--8<---------------cut here---------------start------------->8---
#+name: myinput
| x | 12 |
| y | 24 |
#+BEGIN_SRC ocaml :var x=myinput
x
#+END_SRC
#+RESULTS:
: - : (string * int) array = [|("x", 12); ("y", 24)|]
--8<---------------cut here---------------end--------------->8---
(and the previous example is even nicer:
--8<---------------cut here---------------start------------->8---
#+name: data
| 12 |
| 42 |
#+BEGIN_SRC ocaml :var x=data
x
#+END_SRC
#+RESULTS:
: - : int array = [|12; 42|]
--8<---------------cut here---------------end--------------->8---
)
I have the following questions for the list:
- can I always assume that tables are passed as lists of lists?
- would the patch above be a useful way to deal with this?
- is there a way to specify the :var parsing in a code block or in the table?
Thanks,
Alan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: input data for babel blocks
2013-09-30 12:50 input data for babel blocks Alan Schmitt
@ 2013-09-30 16:26 ` Charles Berry
2013-10-01 0:08 ` Eric Schulte
2013-10-01 8:12 ` Alan Schmitt
2013-10-01 0:04 ` Eric Schulte
1 sibling, 2 replies; 13+ messages in thread
From: Charles Berry @ 2013-09-30 16:26 UTC (permalink / raw)
To: emacs-orgmode
Alan Schmitt <alan.schmitt <at> polytechnique.org> writes:
>
> Hello,
>
> In my quest for analyzing my data in org mode tables, I'm trying to see
> if I can use my favorite language (i.e., ocaml). I'm thus looking at how
[discussion of revising org-babel-variable-assignments:<lang> deleted]
>
> I have the following questions for the list:
> - can I always assume that tables are passed as lists of lists?
> - would the patch above be a useful way to deal with this?
> - is there a way to specify the :var parsing in a code block or in the table?
Alan,
Nice description of a problem that has vexed me, too.
It would be nice to have more flexibility in passing objects to src blocks
in ones favored language via the :var header arg.
Lacking that, another alternative to the approach you have crafted is to
use elisp src blocks to set up the commands needed to create the objects,
and then place the results of executing the elisp src block in the src
block of your favored language using noweb, for example
#+BEGIN_SRC mylang :noweb yes
<<elisp-conversion-to-mylang("arg1","arg2")>>
#+END_SRC
might convert 'arg2' to an object of the desired type named 'arg1' in a
'mylang' src block.
FWIW, my own usage of this approach is to write LaTeX code including
backslashes and unmatched quotes, then convert the code to a valid R
character string for labelling figures (which undoubled backslashes and
unmatched quotes would invalidate).
HTH,
Chuck
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: input data for babel blocks
2013-09-30 12:50 input data for babel blocks Alan Schmitt
2013-09-30 16:26 ` Charles Berry
@ 2013-10-01 0:04 ` Eric Schulte
2013-10-01 8:15 ` Alan Schmitt
1 sibling, 1 reply; 13+ messages in thread
From: Eric Schulte @ 2013-10-01 0:04 UTC (permalink / raw)
To: Alan Schmitt; +Cc: emacs-org list
Alan Schmitt <alan.schmitt@polytechnique.org> writes:
> Hello,
>
> In my quest for analyzing my data in org mode tables, I'm trying to see
> if I can use my favorite language (i.e., ocaml). I'm thus looking at how
> to input such tables in a caml program. I found that the following works
> well:
>
> --8<---------------cut here---------------start------------->8---
> #+name: data
> | 12 |
> | 42 |
>
> #+BEGIN_SRC ocaml :var x=data
> x
> #+END_SRC
>
> #+RESULTS:
> : - : int array array = [|[|12|]; [|42|]|]
> --8<---------------cut here---------------end--------------->8---
>
> (The only "bad" thing is that a single value is an array, but this is
> probably related to the following thing.) Unfortunately (for interaction
> with org mode), caml is strongly typed, and arrays must be
> homogeneous. Thus the following fails:
>
> --8<---------------cut here---------------start------------->8---
> #+name: myinput
> | x | 12 |
> | y | 24 |
>
> #+BEGIN_SRC ocaml :var x=myinput
> x
> #+END_SRC
> --8<---------------cut here---------------end--------------->8---
>
> with the error in the toplevel:
>
> --8<---------------cut here---------------start------------->8---
> # let x = [|[|"x"; 12|]; [|"y"; 24|]|];;
> x;;
> "org-babel-ocaml-eoe";;
> Characters 17-19:
> let x = [|[|"x"; 12|]; [|"y"; 24|]|];;
> ^^
> Error: This expression has type int but an expression was expected of type
> string
> --8<---------------cut here---------------end--------------->8---
>
> I would like to change the parsing of table in ob-ocaml so that it
> generates an array of _tuples_, for instance for the previous example:
>
> #+BEGIN_SRC ocaml
> let x = [| ("x", 12); ("y", 24) |];;
> #+END_SRC
>
> I looked at the code, and this seems to be the relevant part:
>
> #+BEGIN_SRC emacs-lisp
> (defun org-babel-variable-assignments:ocaml (params)
> "Return list of ocaml statements assigning the block's variables."
> (mapcar
> (lambda (pair) (format "let %s = %s;;" (car pair)
> (org-babel-ocaml-elisp-to-ocaml (cdr pair))))
> (mapcar #'cdr (org-babel-get-header params :var))))
>
> (defun org-babel-ocaml-elisp-to-ocaml (val)
> "Return a string of ocaml code which evaluates to VAL."
> (if (listp val)
> (concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml val "; ") "|]")
> (format "%S" val)))
> #+END_SRC
>
> I tried tweaking this to the following:
>
> #+BEGIN_SRC emacs-lisp
> (defun org-babel-variable-assignments:ocaml (params)
> "Return list of ocaml statements assigning the block's variables."
> (mapcar
> (lambda (pair) (format "let %s = %s;;" (car pair)
> (org-babel-ocaml-elisp-to-ocaml (cdr pair))))
> (mapcar #'cdr (org-babel-get-header params :var))))
>
> (defun org-babel-ocaml-elisp-to-ocaml (val)
> "Return a string of ocaml code which evaluates to VAL."
> (if (listp val)
> (concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml-tuple val "; ") "|]")
> (format "%S" val)))
>
> (defun org-babel-ocaml-elisp-to-ocaml-tuple (val)
> "Return a string of ocaml code which evaluates to VAL, as a tuple."
> (if (listp val)
> (concat "(" (mapconcat #'org-babel-ocaml-elisp-to-ocaml-tuple val ", ") ")")
> (format "%S" val)))
> #+END_SRC
>
> Now the example seems to work:
>
> --8<---------------cut here---------------start------------->8---
> #+name: myinput
> | x | 12 |
> | y | 24 |
>
> #+BEGIN_SRC ocaml :var x=myinput
> x
> #+END_SRC
>
> #+RESULTS:
> : - : (string * int) array = [|("x", 12); ("y", 24)|]
> --8<---------------cut here---------------end--------------->8---
>
> (and the previous example is even nicer:
> --8<---------------cut here---------------start------------->8---
> #+name: data
> | 12 |
> | 42 |
>
> #+BEGIN_SRC ocaml :var x=data
> x
> #+END_SRC
>
> #+RESULTS:
> : - : int array = [|12; 42|]
> --8<---------------cut here---------------end--------------->8---
> )
>
> I have the following questions for the list:
> - can I always assume that tables are passed as lists of lists?
Currently this is the default behavior in (I believe) every language.
>
> - would the patch above be a useful way to deal with this?
My problem with the patch above is that it makes OCaml different from
every other language (especially ob-haskell which has similar type
restraints), and that it doesn't work for tables with different
alignment, e.g.,
| x | y |
| 0 | 1 |
I guess one possible "correct" solution would be to use a variant type
with something like the following.
type orgCell =
| Int of int
| Float of float
| String of string
> - is there a way to specify the :var parsing in a code block or in the
> table?
>
Currently there is not. Perhaps there is an elegant solution using a
new header argument to control how values are represented in literal
source code.
This is an interesting question. I'm not sure what is best here, but
ideally any solution will generalize to other strongly typed languages,
will support all possible tables, and will work simply for simple tables
allowing users to use tables without having to jump through typed hoops.
Cheers,
>
> Thanks,
>
> Alan
>
--
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: input data for babel blocks
2013-09-30 16:26 ` Charles Berry
@ 2013-10-01 0:08 ` Eric Schulte
2013-10-01 8:12 ` Alan Schmitt
1 sibling, 0 replies; 13+ messages in thread
From: Eric Schulte @ 2013-10-01 0:08 UTC (permalink / raw)
To: Charles Berry; +Cc: emacs-orgmode
Charles Berry <ccberry@ucsd.edu> writes:
> Alan Schmitt <alan.schmitt <at> polytechnique.org> writes:
>
>>
>> Hello,
>>
>> In my quest for analyzing my data in org mode tables, I'm trying to see
>> if I can use my favorite language (i.e., ocaml). I'm thus looking at how
>
> [discussion of revising org-babel-variable-assignments:<lang> deleted]
>
>>
>> I have the following questions for the list:
>> - can I always assume that tables are passed as lists of lists?
>> - would the patch above be a useful way to deal with this?
>> - is there a way to specify the :var parsing in a code block or in the table?
>
> Alan,
>
> Nice description of a problem that has vexed me, too.
>
> It would be nice to have more flexibility in passing objects to src blocks
> in ones favored language via the :var header arg.
>
> Lacking that, another alternative to the approach you have crafted is to
> use elisp src blocks to set up the commands needed to create the objects,
> and then place the results of executing the elisp src block in the src
> block of your favored language using noweb, for example
>
> #+BEGIN_SRC mylang :noweb yes
> <<elisp-conversion-to-mylang("arg1","arg2")>>
> #+END_SRC
>
> might convert 'arg2' to an object of the desired type named 'arg1' in a
> 'mylang' src block.
>
> FWIW, my own usage of this approach is to write LaTeX code including
> backslashes and unmatched quotes, then convert the code to a valid R
> character string for labelling figures (which undoubled backslashes and
> unmatched quotes would invalidate).
>
> HTH,
>
> Chuck
>
Hi Chuck,
This looks like a very powerful approach, with the added benefit of not
requiring the addition of any new constructs to Org-mode code blocks.
For now I'd say this is the best way to handle these typing problems.
I'd say it would be nice to document this solution somewhere, but I have
no idea where would be a good place for it to live (i.e., where future
users would find it), so hopefully for now text search through the
mailing list will suffice. With that in mind please ignore the
following string of keywords which are purely for SEO.
<keywords>
table type string integer number cell tuple strongly typed haskell ocaml
</keywords>
Cheers,
--
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: input data for babel blocks
2013-09-30 16:26 ` Charles Berry
2013-10-01 0:08 ` Eric Schulte
@ 2013-10-01 8:12 ` Alan Schmitt
2013-10-01 11:58 ` Eric Schulte
2013-10-01 15:29 ` Charles Berry
1 sibling, 2 replies; 13+ messages in thread
From: Alan Schmitt @ 2013-10-01 8:12 UTC (permalink / raw)
To: Charles Berry; +Cc: emacs-orgmode
Hi Charles,
ccberry@ucsd.edu writes:
> Lacking that, another alternative to the approach you have crafted is to
> use elisp src blocks to set up the commands needed to create the objects,
> and then place the results of executing the elisp src block in the src
> block of your favored language using noweb, for example
>
> #+BEGIN_SRC mylang :noweb yes
> <<elisp-conversion-to-mylang("arg1","arg2")>>
> #+END_SRC
>
> might convert 'arg2' to an object of the desired type named 'arg1' in a
> 'mylang' src block.
This looks like a very powerful approach, but it's a bit beyond my
understanding of babel (which is limited) and noweb (whose existence I
just discovered after reading http://orgmode.org/manual/noweb.html).
The way I understand it is:
- there is somewhere in the file a "elisp-conversion-to-mylang"
function;
- upon export or evaluation or tangling, it will be expanded in the body
of the source block;
- it will then be evaluated in the source block.
What I don't understand is:
- how to define this function;
- will it be evaluated as a "mylang" function or as a function in the
language it is defined?
In other words, do we have "evaluate elisp-conversion-to-mylang in its
language then substitute the results in the noweb block" or "substitute
the function in the noweb block then evaluate it"?
If you have an example that uses different languages, I'd love to look
at it. I'll then try to write an example for ocaml.
Thanks,
Alan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: input data for babel blocks
2013-10-01 0:04 ` Eric Schulte
@ 2013-10-01 8:15 ` Alan Schmitt
0 siblings, 0 replies; 13+ messages in thread
From: Alan Schmitt @ 2013-10-01 8:15 UTC (permalink / raw)
To: Eric Schulte; +Cc: emacs-org list
Hi Eric,
schulte.eric@gmail.com writes:
>> - would the patch above be a useful way to deal with this?
>
> My problem with the patch above is that it makes OCaml different from
> every other language (especially ob-haskell which has similar type
> restraints), and that it doesn't work for tables with different
> alignment, e.g.,
>
> | x | y |
> | 0 | 1 |
>
> I guess one possible "correct" solution would be to use a variant type
> with something like the following.
>
> type orgCell =
> | Int of int
> | Float of float
> | String of string
Yes, this would be more flexible. However, this type definition must
occur only once (when the session is launched). Looking at the code, I'm
not sure how to ensure that.
>> - is there a way to specify the :var parsing in a code block or in the
>> table?
>>
>
> Currently there is not. Perhaps there is an elegant solution using a
> new header argument to control how values are represented in literal
> source code.
>
> This is an interesting question. I'm not sure what is best here, but
> ideally any solution will generalize to other strongly typed languages,
> will support all possible tables, and will work simply for simple tables
> allowing users to use tables without having to jump through typed hoops.
I'll try to apply Charles's suggestion, once I'm able to fully
understand it, and I'll report it here.
Alan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: input data for babel blocks
2013-10-01 8:12 ` Alan Schmitt
@ 2013-10-01 11:58 ` Eric Schulte
2013-10-01 13:01 ` Alan Schmitt
2013-10-01 15:29 ` Charles Berry
1 sibling, 1 reply; 13+ messages in thread
From: Eric Schulte @ 2013-10-01 11:58 UTC (permalink / raw)
To: Alan Schmitt; +Cc: emacs-orgmode, Charles Berry
[-- Attachment #1: Type: text/plain, Size: 1700 bytes --]
Alan Schmitt <alan.schmitt@polytechnique.org> writes:
> Hi Charles,
>
> ccberry@ucsd.edu writes:
>
>> Lacking that, another alternative to the approach you have crafted is to
>> use elisp src blocks to set up the commands needed to create the objects,
>> and then place the results of executing the elisp src block in the src
>> block of your favored language using noweb, for example
>>
>> #+BEGIN_SRC mylang :noweb yes
>> <<elisp-conversion-to-mylang("arg1","arg2")>>
>> #+END_SRC
>>
>> might convert 'arg2' to an object of the desired type named 'arg1' in a
>> 'mylang' src block.
>
> This looks like a very powerful approach, but it's a bit beyond my
> understanding of babel (which is limited) and noweb (whose existence I
> just discovered after reading http://orgmode.org/manual/noweb.html).
>
> The way I understand it is:
> - there is somewhere in the file a "elisp-conversion-to-mylang"
> function;
> - upon export or evaluation or tangling, it will be expanded in the body
> of the source block;
> - it will then be evaluated in the source block.
>
> What I don't understand is:
> - how to define this function;
It is not a function name, it is a code block name. See the noweb
section of the Org-mode manual for more information.
>
> - will it be evaluated as a "mylang" function or as a function in the
> language it is defined?
>
> In other words, do we have "evaluate elisp-conversion-to-mylang in its
> language then substitute the results in the noweb block" or "substitute
> the function in the noweb block then evaluate it"?
>
see above
>
> If you have an example that uses different languages, I'd love to look
> at it. I'll then try to write an example for ocaml.
>
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: noweb-variable-example.org --]
[-- Type: text/x-org, Size: 378 bytes --]
#+name: data
| x | 2 |
| y | 4 |
#+name: table-as-array
#+begin_src sh :var data=data :results verbatim
echo "$data"|sed 's/\s/ /;s/^/ [/;s/$/]/;1 s/^\s\+/(setf it [/; $ s/$/])/'
#+end_src
#+RESULTS: table-as-array
: (setf it [[x 2]
: [y 4]])
#+begin_src emacs-lisp :noweb yes
<<table-as-array(data)>>
(aref (aref it 1) 1)
#+end_src
#+RESULTS:
: 4
[-- Attachment #3: Type: text/plain, Size: 105 bytes --]
Hope this Helps,
>
> Thanks,
>
> Alan
>
--
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: input data for babel blocks
2013-10-01 11:58 ` Eric Schulte
@ 2013-10-01 13:01 ` Alan Schmitt
2013-10-01 14:29 ` Rick Frankel
0 siblings, 1 reply; 13+ messages in thread
From: Alan Schmitt @ 2013-10-01 13:01 UTC (permalink / raw)
To: Eric Schulte; +Cc: emacs-orgmode, Charles Berry
Hi Eric,
schulte.eric@gmail.com writes:
>> What I don't understand is:
>> - how to define this function;
>
> It is not a function name, it is a code block name. See the noweb
> section of the Org-mode manual for more information.
OK.
>> - will it be evaluated as a "mylang" function or as a function in the
>> language it is defined?
>>
>> In other words, do we have "evaluate elisp-conversion-to-mylang in its
>> language then substitute the results in the noweb block" or "substitute
>> the function in the noweb block then evaluate it"?
>>
>
> see above
I'm sorry, I don't see the answer to this above. The only example I
could find in the manual is this one
http://orgmode.org/manual/noweb_002dref.html#noweb_002dref which does
not address using noweb with different languages.
I did some experiments and I'm even more confused. Here is a test where
I want to feed the results of "ls" in a shell block as an ocaml
list. This is what I tried:
--8<---------------cut here---------------start------------->8---
#+BEGIN_SRC sh :noweb-ref testing
echo "["
for i in `ls`; do
echo \"$i;\"
done
echo "]"
#+END_SRC
#+BEGIN_SRC ocaml :noweb yes
let x =
<<testing>>
in x
#+END_SRC
--8<---------------cut here---------------end--------------->8---
This is clearly wrong because this is what ends up in the toplevel:
--8<---------------cut here---------------start------------->8---
let x =
echo "["
for i in `ls`; do
echo \"$i;\"
done
echo "]"
in x;;
--8<---------------cut here---------------end--------------->8---
(I would have like the code to be executed, and the raw results to
replace the <<testing>> ref.)
So I'll rephrase my question: how can I use noweb with different
languages?
Thanks,
Alan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: input data for babel blocks
2013-10-01 13:01 ` Alan Schmitt
@ 2013-10-01 14:29 ` Rick Frankel
2013-10-01 15:16 ` Alan Schmitt
0 siblings, 1 reply; 13+ messages in thread
From: Rick Frankel @ 2013-10-01 14:29 UTC (permalink / raw)
To: Alan Schmitt; +Cc: emacs-orgmode, Charles Berry, Eric Schulte
On 2013-10-01 09:01, Alan Schmitt wrote:
> I'm sorry, I don't see the answer to this above. The only example I
> could find in the manual is this one
> http://orgmode.org/manual/noweb_002dref.html#noweb_002dref which does
> not address using noweb with different languages.
>
> I did some experiments and I'm even more confused. Here is a test where
> I want to feed the results of "ls" in a shell block as an ocaml
> list. This is what I tried:
>
> #+BEGIN_SRC sh :noweb-ref testing
> echo "["
> for i in `ls`; do
> echo \"$i;\"
> done
> echo "]"
> #+END_SRC
>
> #+BEGIN_SRC ocaml :noweb yes
> let x =
> <<testing>>
> in x
> #+END_SRC
>
> This is clearly wrong because this is what ends up in the toplevel:
>
> let x =
> echo "["
> for i in `ls`; do
> echo \"$i;\"
> done
> echo "]"
> in x;;
>
You're close. The noweb ref should be a named src block which is
executed, not expanded, so, (note the named shell source block and the
parens in the noweb reference):
#+name: testing
#+BEGIN_SRC sh :results raw
echo "["
ls *.org | sed 's/$/;/'
echo "]"
#+END_SRC
#+BEGIN_SRC ocaml :noweb yes
let x =
<<testing()>>
in x
#+END_SRC
rick
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: input data for babel blocks
2013-10-01 14:29 ` Rick Frankel
@ 2013-10-01 15:16 ` Alan Schmitt
0 siblings, 0 replies; 13+ messages in thread
From: Alan Schmitt @ 2013-10-01 15:16 UTC (permalink / raw)
To: Rick Frankel; +Cc: emacs-orgmode, Charles Berry, Eric Schulte
rick@rickster.com writes:
> You're close. The noweb ref should be a named src block which is
> executed, not expanded, so, (note the named shell source block and the
> parens in the noweb reference):
>
> #+name: testing
> #+BEGIN_SRC sh :results raw
> echo "["
> ls *.org | sed 's/$/;/'
> echo "]"
> #+END_SRC
>
> #+BEGIN_SRC ocaml :noweb yes
> let x =
> <<testing()>>
> in x
> #+END_SRC
Thanks a lot, that was the missing piece!
So, for the record, here is a way to convert a table as a list of tuples
for use in ocaml. Thanks again to everyone for the help in getting
there.
--8<---------------cut here---------------start------------->8---
#+name: mydata
| x | 1 | 1.4 |
| y | 2 | 4.5 |
| z | 3 | 7.0 |
#+name: table_to_tuple
#+BEGIN_SRC emacs-lisp :results raw :var v='()
(message
(concat "["
(mapconcat
(lambda (vlist)
(concat "("
(mapconcat
(lambda (val) (format "%S" val))
vlist
", ")
")"))
v
"; ")
"]"))
#+END_SRC
#+BEGIN_SRC ocaml :noweb yes
let x =
<<table_to_tuple(mydata)>>
in x
#+END_SRC
#+RESULTS:
: - : (string * int * float) list =
: [("x", 1, 1.4); ("y", 2, 4.5); ("z", 3, 7.)]
--8<---------------cut here---------------end--------------->8---
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: input data for babel blocks
2013-10-01 8:12 ` Alan Schmitt
2013-10-01 11:58 ` Eric Schulte
@ 2013-10-01 15:29 ` Charles Berry
2013-10-01 17:16 ` Alan Schmitt
2013-10-01 19:06 ` Thomas S. Dye
1 sibling, 2 replies; 13+ messages in thread
From: Charles Berry @ 2013-10-01 15:29 UTC (permalink / raw)
To: emacs-orgmode
Alan Schmitt <alan.schmitt <at> polytechnique.org> writes:
>
> Hi Charles,
>
> ccberry <at> ucsd.edu writes:
>
> > Lacking that, another alternative to the approach you have crafted is to
> > use elisp src blocks to set up the commands needed to create the
> > objects,
> > and then place the results of executing the elisp src block in the src
> > block of your favored language using noweb, for example
> >
> > #+BEGIN_SRC mylang :noweb yes
> > <<elisp-conversion-to-mylang("arg1","arg2")>>
> > #+END_SRC
> >
> > might convert 'arg2' to an object of the desired type named 'arg1' in a
> > 'mylang' src block.
>
> This looks like a very powerful approach, but it's a bit beyond my
> understanding of babel (which is limited) and noweb (whose existence I
> just discovered after reading http://orgmode.org/manual/noweb.html).
>
Sorry if it was a bit obtuse. The examples given by others seem to have
helped. And I will give one more.
[deleted]
> If you have an example that uses different languages, I'd love to look
> at it. I'll then try to write an example for ocaml.
>
Here is what I use for LaTeX thru elisp to R:
* Quote Blocks
quote-blks takes two args:
- blk :: a string of comma separated src block names
- sep :: an optional separator for use when there is more than one
block
I usually save it in a file and load it in with
(org-babel-lob-ingest file). But you can copy and paste and the example
below will still work.
#+name: quote-blks
#+BEGIN_SRC emacs-lisp :var blk="abc" :var sep="\"\n\""
(save-excursion
(replace-regexp-in-string "\"\"" ""
(mapconcat
(lambda (x)
(org-babel-goto-named-src-block x)
(format "%S" (cadr (org-babel-get-src-block-info t))))
(split-string blk "," t)
sep)
t t))
#+END_SRC
* example of use
The LaTeX here can be editted via C-c '
(i.e. org-edit-special --> org-edit-src-code).
#+name: lstuff
#+BEGIN_SRC latex :eval never :exports none
Here is a \backslash. And an unmatched quote: '.
#+END_SRC
Here is an example using it on the above block.
The cat statement prints a formatted version. The str statement shows
what R sees (but the outer quotes are not part of the string).
#+BEGIN_SRC R :noweb yes :results output
some.latex <-
<<quote-blks("lstuff")>>
cat(some.latex,"\n")
str(some.latex)
#+END_SRC
#+RESULTS:
: Here is a \backslash. And an unmatched quote: '.
: chr "Here is a \\backslash. And an unmatched quote: '."
HTH,
Chuck
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: input data for babel blocks
2013-10-01 15:29 ` Charles Berry
@ 2013-10-01 17:16 ` Alan Schmitt
2013-10-01 19:06 ` Thomas S. Dye
1 sibling, 0 replies; 13+ messages in thread
From: Alan Schmitt @ 2013-10-01 17:16 UTC (permalink / raw)
To: Charles Berry; +Cc: emacs-orgmode
ccberry@ucsd.edu writes:
> Here is what I use for LaTeX thru elisp to R:
Thanks, this is quite enlightening.
Alan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: input data for babel blocks
2013-10-01 15:29 ` Charles Berry
2013-10-01 17:16 ` Alan Schmitt
@ 2013-10-01 19:06 ` Thomas S. Dye
1 sibling, 0 replies; 13+ messages in thread
From: Thomas S. Dye @ 2013-10-01 19:06 UTC (permalink / raw)
To: Charles Berry; +Cc: emacs-orgmode
Hi Chuck,
Neat. Thanks for sharing.
All the best,
Tom
Charles Berry <ccberry@ucsd.edu> writes:
> Alan Schmitt <alan.schmitt <at> polytechnique.org> writes:
>
>>
>> Hi Charles,
>>
>> ccberry <at> ucsd.edu writes:
>>
>> > Lacking that, another alternative to the approach you have crafted is to
>> > use elisp src blocks to set up the commands needed to create the
>> > objects,
>> > and then place the results of executing the elisp src block in the src
>> > block of your favored language using noweb, for example
>> >
>> > #+BEGIN_SRC mylang :noweb yes
>> > <<elisp-conversion-to-mylang("arg1","arg2")>>
>> > #+END_SRC
>> >
>> > might convert 'arg2' to an object of the desired type named 'arg1' in a
>> > 'mylang' src block.
>>
>> This looks like a very powerful approach, but it's a bit beyond my
>> understanding of babel (which is limited) and noweb (whose existence I
>> just discovered after reading http://orgmode.org/manual/noweb.html).
>>
>
> Sorry if it was a bit obtuse. The examples given by others seem to have
> helped. And I will give one more.
>
> [deleted]
>
>> If you have an example that uses different languages, I'd love to look
>> at it. I'll then try to write an example for ocaml.
>>
>
>
> Here is what I use for LaTeX thru elisp to R:
>
>
>
> * Quote Blocks
>
> quote-blks takes two args:
>
> - blk :: a string of comma separated src block names
> - sep :: an optional separator for use when there is more than one
> block
>
> I usually save it in a file and load it in with
> (org-babel-lob-ingest file). But you can copy and paste and the example
> below will still work.
>
> #+name: quote-blks
> #+BEGIN_SRC emacs-lisp :var blk="abc" :var sep="\"\n\""
> (save-excursion
> (replace-regexp-in-string "\"\"" ""
> (mapconcat
> (lambda (x)
> (org-babel-goto-named-src-block x)
> (format "%S" (cadr (org-babel-get-src-block-info t))))
> (split-string blk "," t)
> sep)
> t t))
>
> #+END_SRC
>
>
> * example of use
>
>
> The LaTeX here can be editted via C-c '
> (i.e. org-edit-special --> org-edit-src-code).
>
> #+name: lstuff
> #+BEGIN_SRC latex :eval never :exports none
> Here is a \backslash. And an unmatched quote: '.
> #+END_SRC
>
>
> Here is an example using it on the above block.
>
> The cat statement prints a formatted version. The str statement shows
> what R sees (but the outer quotes are not part of the string).
>
> #+BEGIN_SRC R :noweb yes :results output
> some.latex <-
> <<quote-blks("lstuff")>>
> cat(some.latex,"\n")
> str(some.latex)
> #+END_SRC
>
> #+RESULTS:
> : Here is a \backslash. And an unmatched quote: '.
> : chr "Here is a \\backslash. And an unmatched quote: '."
>
> HTH,
>
> Chuck
>
>
>
--
Thomas S. Dye
http://www.tsdye.com
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2013-10-01 19:08 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-09-30 12:50 input data for babel blocks Alan Schmitt
2013-09-30 16:26 ` Charles Berry
2013-10-01 0:08 ` Eric Schulte
2013-10-01 8:12 ` Alan Schmitt
2013-10-01 11:58 ` Eric Schulte
2013-10-01 13:01 ` Alan Schmitt
2013-10-01 14:29 ` Rick Frankel
2013-10-01 15:16 ` Alan Schmitt
2013-10-01 15:29 ` Charles Berry
2013-10-01 17:16 ` Alan Schmitt
2013-10-01 19:06 ` Thomas S. Dye
2013-10-01 0:04 ` Eric Schulte
2013-10-01 8:15 ` Alan Schmitt
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).