Giter VIP home page Giter VIP logo

Comments (9)

magnars avatar magnars commented on August 10, 2024

I think that looks brilliant. But maybe as a separate function, so you can ditch the 'lex-value all together?

(let ((x 1))
  (s-lex-format "x is ${x}"))

from s.el.

nicferrier avatar nicferrier commented on August 10, 2024

The only trouble with that is that I think there are two things that
would be useful, lexical access and lexical access with Lisp output:

I'm using this code right now:

(defvar lexical-value-when-dynamic :error
  "How `lexical-value' should act in dynamic scope.

Valid values are `:error', which is the default and causes
`lexical-value' to error when called in dynamic scope, or
`:symbol-value' which causes the `symbol-value' function to be
called for the symbol or `nil' which causes nil to be returned.")

(defmacro lexical-value (lexical-symbol)
  "What is the value of the LEXICAL-SYMBOL specified?"
  (let ((pv (make-symbol "pv"))
        (sv (make-symbol "snv")))
    `(let ((,pv (lambda ())))
       (if (eq 'closure (car ,pv))
           (let ((value 
                  (assoc ,lexical-symbol (cadr ,pv))))
             (when value (cdr value)))
           (case lexical-value-when-dynamic
             (:error (error "not in a lexical environment"))
             (:symbol-value (symbol-value ,lexical-symbol))
             (t nil))))))

(defmacro with-sformat-lex (&rest body)
  (declare (debug (&rest form))
           (indent 0))
  `(flet ((lex-val (var)
            (let ((lexical-value-when-dynamic :symbol-value)
                  (variable (intern var)))
              (lexical-value variable)))
          (lisp-val (var) (format "%S" (lex-val var))))
     ,@body))

So I can go:

(let ((x 1)
      (y '(a b c)))
 (with-sformat-lex
   (format "x is: ${x} and y is: ${y}" 'lisp-val)))

or I could use lex-val.

I guess I could start adding more dynamic options to format to control
how things are output?

from s.el.

magnars avatar magnars commented on August 10, 2024

What I want are fewer dynamic options and instead a cleaner API. Functions
that do one thing.

On Mon, Apr 29, 2013 at 3:32 PM, Nic Ferrier [email protected]:

The only trouble with that is that I think there are two things that
would be useful, lexical access and lexical access with Lisp output:

I'm using this code right now:

(defvar lexical-value-when-dynamic :error
"How `lexical-value' should act in dynamic scope.

Valid values are `:error', which is the default and causes
`lexical-value' to error when called in dynamic scope, or
`:symbol-value' which causes the `symbol-value' function to be
called for the symbol or `nil' which causes nil to be returned.")

(defmacro lexical-value (lexical-symbol)
"What is the value of the LEXICAL-SYMBOL specified?"
(let ((pv (make-symbol "pv"))
(sv (make-symbol "snv")))
`(let ((,pv (lambda ())))
(if (eq 'closure (car ,pv))
(let ((value
(assoc ,lexical-symbol (cadr ,pv))))
(when value (cdr value)))
(case lexical-value-when-dynamic
(:error (error "not in a lexical environment"))
(:symbol-value (symbol-value ,lexical-symbol))
(t nil))))))

(defmacro with-sformat-lex (&rest body)
(declare (debug (&rest form))
(indent 0))
`(flet ((lex-val (var)
(let ((lexical-value-when-dynamic :symbol-value)
(variable (intern var)))
(lexical-value variable)))
(lisp-val (var) (format "%S" (lex-val var))))
,@body))

So I can go:

(let ((x 1)
(y '(a b c)))
(with-sformat-lex
(format "x is: ${x} and y is: ${y}" 'lisp-val)))

or I could use lex-val.

I guess I could start adding more dynamic options to format to control
how things are output?


Reply to this email directly or view it on GitHubhttps://github.com//issues/27#issuecomment-17166575
.

from s.el.

nicferrier avatar nicferrier commented on August 10, 2024

Do you? I like all that being able to change the behaviour of things by let binding and such. It's fun.

from s.el.

nicferrier avatar nicferrier commented on August 10, 2024

How about this:

(defmacro s-lex-format (format-str)
  (let ((pv (make-symbol "pv")))
    `(s-format
      ,format-str
      (let ((,pv (lambda ())))
        (if (eq 'closure (car ,pv))
            (lambda (var-name)
              (let ((value 
                     (assoc (intern var-name) (cadr ,pv))))
                (when value (cdr value))))
            (lambda (var-name)
              (case lexical-value-when-dynamic
                (:error (error "not in a lexical environment"))
                (:symbol-value (symbol-value
                                (intern var-name)))
                (t nil))))))))

(let ((lexical-value-when-dynamic :symbol-value)
      (x "1"))
  (s-lex-format "${x}"))

My only concern with this is that often you want the lex value to be lisp printed ("%S" instead of "%s") and that's not possible here. It could be done with a switch or a dyn var.

from s.el.

magnars avatar magnars commented on August 10, 2024

It's too convoluted for me. I would be a happy man if I could just have a formatting function that grabs content off the current scope. But come to think of it, maybe that's just concat, with " " separating the keys instead of ${}. How is ${} any better?

from s.el.

nicferrier avatar nicferrier commented on August 10, 2024

For all the reasons that s-format is better. My use case is this:

;;; ${name}.el --- ${desc}

${copy}

${decl}

;;; Commentary:

${commentary}

;;; Code:

${defn-code}

${prvide-code}
;;; ${name}.el ends here

Code that with concat and see how happy you are :-)

from s.el.

magnars avatar magnars commented on August 10, 2024

Haha, you're right. That would be terrible. :-)

On Mon, Apr 29, 2013 at 11:18 PM, Nic Ferrier [email protected]:

For all the reasons that s-format is better. My use case is this:

;;; ${name}.el --- ${desc}

${copy}

${decl}

;;; Commentary:

${commentary}

;;; Code:

${defn-code}

${prvide-code}
;;; ${name}.el ends here

Code that with concat and see how happy you are :-)


Reply to this email directly or view it on GitHubhttps://github.com//issues/27#issuecomment-17195146
.

from s.el.

nicferrier avatar nicferrier commented on August 10, 2024

It seems like a standard templating problem to me. And I've never really got how we can fix that with simple Lisp. Maybe there is a better solution but I'm not aware of it.

from s.el.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.