14

I have a functions written in an external language called GO. How can I quickly load them into the kernel for use as Mathematica functions?

The two ways I've tried are with MathLink and LibraryLink and it's overly complicated, however in version 10 there is RunProcess[]

Can anyone find a way of using this new feature to install an external function?

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
user5601
  • 3,573
  • 2
  • 24
  • 56
  • Or any other language for that matter... Seems like using the operating system piping with this function is the easiest way to link in functions no? – user5601 Nov 19 '14 at 03:49
  • I've heard that some internal functions were constructed like this. The alternative methods involve writing c-drivers for the linkers and can be hairy... – M.R. Nov 19 '14 at 18:35
  • @M.R. very interesting! which ones? – user5601 Nov 19 '14 at 19:48
  • @m.R. SemanticImport indeed uses "ProcessLink" (the family of functions like RunProcess, StartProcess, etc.) Indeed it is also written in Go. – Taliesin Beynon Nov 19 '14 at 23:18
  • @TaliesinBeynon Cool! If you would please give a little showcase example of how this works that would be great and I'll accept it :) – user5601 Nov 19 '14 at 23:31

1 Answers1

26

Taking user5601's suggestion to do a little demo, I quickly whipped this up as an example of ProcessLink being used to do non-trivial communication between Mathematica and an external program, but with much less ceremony than using ProcessLink or MathLink.

Let's take this little Go program:

package main

import "net/http" import "bufio" import "os" import "fmt" import "html" import "strings"

func main() { http.ListenAndServe(":8080", http.HandlerFunc(render)) }

func render(w http.ResponseWriter, req *http.Request) { var img, response, input string input = req.FormValue("input") // extract the input from the page if input != "" { fmt.Println(input) // send to mathematica reader := bufio.NewReader(os.Stdin) // get back result img, _ = reader.ReadString('\n') img = strings.TrimSpace(img) img = <img src="data:image/png;base64, + img + "/> } response = <html><body><form action="result.html">Enter a Wolfram Language expression:<br><input type="text" name="input" size="65" value=" + html.EscapeString(input) + "></form><br>The result was:<br> + img + </body></html> w.Write([]byte(response)) // write back the new page }

And this Mathematica program:

server = StartProcess["/Users/taliesinb/processLinkExample/main"]
While[True,
  input = ReadLine[server];
  If[input == "Quit[]", KillProcess[server]; Break[]];
  Print["Executing ", input];
  result = ToExpression[input];
  img = Developer`EncodeBase64[ExportString[Rasterize[result, ImageSize -> Small], "PNG"]];
  WriteLine[server, img]]

You will get a tiny web-server running on localhost:8080 that you can visit in your browser. Here's an example of what it will do:

enter image description here

Brought to you by ProcessLink (tm)!

AsukaMinato
  • 9,758
  • 1
  • 14
  • 40
Taliesin Beynon
  • 10,639
  • 44
  • 51