TW equivalents of some simple expressions in common programming langauges

171 weergaven
Naar het eerste ongelezen bericht

Joe Armstrong

ongelezen,
18 mrt 2018, 18:01:5818-03-2018
aan TiddlyWiki
I am having trouble relating what I know in a large number of
programming languages to the syntax and semantics of the 
tiddler language.

For example, after searching for about half an hour I could not
find out how to perform an if-then-else operation.

Since 95% of all imperative programs are made
by assigning values (simple and complex) to variables,
unpacking these and performing conditionals, loops and
function calls I'd like to see exactly how to perform
these operations in a tiddler.

If I were writing a book on programming I'd start
with variables and values and move on to conditionals
and loops, then show how to write procedures.

So far it is unclear to me how to do these things in the TW language.

So, what is the equivalent tiddler code to perform the following
operations in a conventional programming languages?

1) Set a variable to a simple value and use the value

2) Set a variable to a complex value  and use the complex value

3) Perform an if-then-else or switch operation

4) Execute a loop

5) Define a local (to the tiddler) subroutine and call it

6) Define a global subroutine and call it

It seems to me that a simple table with two columns
showing in the left hand a common a common programming
construct and the right hand side the equivalent Tiddler code would
made TW a lot easier to lean.

Cheers

/Joe




Jed Carty

ongelezen,
18 mrt 2018, 19:18:0918-03-2018
aan TiddlyWiki
Wikitext isn't a Turing completely language, there aren't necessarily equivalents to what you are talking about. TiddlyWiki has very few attributes of a normal programming language, I don't think that tiddlywiki has the equivalent of a procedure from an imperative programming language. You can use javascript in tiddlywiki, but in that case it is javascript, not anything specific to tiddlywiki.

TiddlyWiki does allow a lot of flexibility using recursion and, in my opinion at least, is something like declarative or functional programming.


That said, you can do if-then-else using lists like this:

<$list filter='condition statement' emptyMessage=<<Else result>>>
Than result
</$list>

as long as you can define your condition as a filter that returns one or more results if true and no results if false. If you are comfortable with set theory than the filtering operations in tiddlywiki are very powerful.

TonyM

ongelezen,
19 mrt 2018, 00:06:0119-03-2018
aan TiddlyWiki
Joe,

Jed is highlighting something I posted about for you elsewhere. Tiddlywiki is not a procedural language, it works to link and update every relationship on every change. This is its strength and the reason people including myself, with expirence of proceedural language algorithims, initaly struggle with the concepts.

It is however all worthwhile, I liken it to taking the red pill not the blue one (the matrix movie reference).

Not withstanding the above points i believe the richest source of what you are asking for can be found in Evans formula plugin.

Tiddlywiki is a quine, a platform, a user interface, a database, a document, a website and more all in one.

TiddlyWiki demands we suspend some preconceptions so you can learn it enought, that you then apply prior knowledge to it.

Yours sincerely
Tony

TonyM

ongelezen,
19 mrt 2018, 00:11:0019-03-2018
aan TiddlyWiki
Ps
I will try and respond to each of the items you raise when I finish driving to melbourne.

Give you a personal orientation. Look for my previouse post to you.

Tony

PMario

ongelezen,
19 mrt 2018, 05:12:2519-03-2018
aan TiddlyWiki
On Sunday, March 18, 2018 at 11:01:58 PM UTC+1, Joe Armstrong wrote:
So, what is the equivalent tiddler code to perform the following
operations in a conventional programming languages?

1) Set a variable to a simple value and use the value

a) In TW "variables" are scoped.
b) The scope boundary is the widget, that defines the variable

c) For historic reasons there are 2 dedicated widgets that set variables only.
d) <$set - widget and
e) <$vars - widget

<$set existed first ... Since defining several variables can be cumbersome using nested set-widgets,
<$vars was created in TW version 5.1.9

See: https://tiddlywiki.com/#VarsWidget  ... It also explains the differences between setWidget and varsWidget

-------------------

simple example:

<$set name="local-scope" value="known in $set boundary only">

<
<local-scope>>

</$set>



2) Set a variable to a complex value  and use the complex value

If we create a macro at the beginning of the tiddler like in the following code, we can define a
tiddler-local  or tiddler-global scope:

\define tiddler-scope() known within a tiddler

<$set name="local-scope" value="known in $set boundary only">

<<tiddler-scope>>

<<local-scope>>

</$set>

---

<<tiddler-scope>>

<<local-scope>> ... is silently ignored.


Internally the \define macro() section is converted to a <$set widget ... as I wrote in an other post:

------------------

To create "system-wide" global scope you'll need to create a tiddler
named eg: myMacros    and
tag it: $:/tags/Macro

see: https://tiddlywiki.com/#Macros

Internally: At the beginning of every tiddler a <$importvariables .. widget is called, which "imports" every tiddler tagged $:/tags/Macro available within a tidder scope.

see: https://tiddlywiki.com/#ImportVariablesWidget   section: Global Macros

This code is defined in $:/core/ui/PageTemplate ... as described in the link above.

have fun!
mario


PMario

ongelezen,
19 mrt 2018, 05:21:0419-03-2018
aan TiddlyWiki
On Sunday, March 18, 2018 at 11:01:58 PM UTC+1, Joe Armstrong wrote:

3) Perform an if-then-else or switch operation

In TW if-then most of the time is: "show something" or "hide something" if a special condition is matched.

Since the wikitext is automatically refreshed, if the "tiddler-store" is changed, most of the time compared values are stored in tddlers.

see: https://tiddlywiki.com/#RevealWidget  

A second possibility to create  conditions is using the <$list widget, as Jed described. But this mechanism is a bit cumbersome.

There is an existing pull request at github, which should extend the functionality.
see: https://github.com/Jermolene/TiddlyWiki5/pull/3157

-mario

PMario

ongelezen,
19 mrt 2018, 05:41:2019-03-2018
aan tiddl...@googlegroups.com
On Sunday, March 18, 2018 at 11:01:58 PM UTC+1, Joe Armstrong wrote:

4) Execute a loop

In TW everything, that needs looping is done with the <$list widget.

It's main purpose is to iterate over a defined set of tiddlers.
The set of tiddlers is defined using the filter-attribute. eg:

<$list filter="one two three">
<
<currentTiddler>>,
</$list>


The "loop-variable" can be defined:

<$list filter="one two three" variable="loop-var">
<
<loop-var>>,
</$list>


The list widget can be nested:

<$list filter="one two three" variable="outer">
  <$list filter="a b c" variable="inner">

   <
<outer>>-<<inner>>
 
</$list>
</$list>


Lists can be super complex. ... So these examples, only scratch the surface of the possibilities.

have fun!
mario

PMario

ongelezen,
19 mrt 2018, 05:47:4619-03-2018
aan TiddlyWiki
On Sunday, March 18, 2018 at 11:01:58 PM UTC+1, Joe Armstrong wrote:

5) Define a local (to the tiddler) subroutine and call it

6) Define a global subroutine and call it

see: my first post with macros.


It seems to me that a simple table with two columns
showing in the left hand a common a common programming
construct and the right hand side the equivalent Tiddler code would
made TW a lot easier to lean.

Hmm, Im not sure if that's possible, in a way that makes sense.

I don't think there can be a 1 to 1 comparison. Tiddlywiki syntax is primarily used to render textual user content. ..

It's not a general purpose programming language.

It's a highly specialized DSL (Domain Specific Language), that allows users to create dynamically rendered text. ... and what makes it special. The same syntax is used to render the UI. ...

- mario

TonyM

ongelezen,
19 mrt 2018, 06:05:1219-03-2018
aan TiddlyWiki
I will add here if the filter read

A b b c

You would get A b c

Or

A b c A
You would get A b c

Because as I conceptualise it the "titles" are deduplicated, because titles are unique.

Why because if we had a filter that included
taga or tagb
We would not want the same tiddler title listed twice because its tagged both taga and tagb

Regards
Tony

TonyM

ongelezen,
19 mrt 2018, 06:18:3619-03-2018
aan TiddlyWiki
Also

If a variable is set to another variable such as <<currentTiddler>> the value is not frozen as the value at the time you set it, but will return the name of the current tiddler in the context it is used.

You can apparently use wikify to freeze the variable.

Others can say this better.

Tony

Joe Armstrong

ongelezen,
19 mrt 2018, 06:36:3019-03-2018
aan TiddlyWiki
I should explain my question a little more.

What I wanted to in pseudo code was something like this:

If (this_tiddler_has_tag("foo")) {
    <h1>Hello I'm a foo</h1>
    <button>Click</button>
} else {
    <h1>Hello</h1>
 }

Now there are 10 quadzilion template languages that can do this.
For example in django I might write

{% if has_tag("foo") %}
   <h1>hello I'm a foo</h1>
   ...
{% else %}
   <h1>hello<>h1>
{% endif %}

Pretty easy for anybody to understand - the django filter language is described

(I'm not recommending django BTW - it's for the example)

The point is this type of syntax (call it django, php, etc.) is
vary familiar.

The <$list ..> syntax is as far as I can see ,what in many other languages would be called
a list comprehension. Some language call these iterators.

I'd have expected a syntax link this:

    <? for i in tiddlers_with_tag("foo") && tiddlers_with_tag("bar") ?>

        <h1> ${i).title </h1>

    <? endfor ?>

The TW equivalent has no variable i but seems to have an implicit variable
<<currentTiddler>> which is somewhat confusing.

As it is the similarity to HTML leads me to believe that the widget is
something like HTML (which it is not) - rather than a programming
language construct (which it is)

Strange syntax frightens beginners off - they feel happier
with familiar syntax.


On Monday, 19 March 2018 00:18:09 UTC+1, Jed Carty wrote:
Wikitext isn't a Turing completely language, there aren't necessarily equivalents to what you are talking about. TiddlyWiki has very few attributes of a normal programming language, I don't think that tiddlywiki has the equivalent of a procedure from an imperative programming language. You can use javascript in tiddlywiki, but in that case it is javascript, not anything specific to tiddlywiki.


Actually wikiText could be Turing complete (and possible easier to understand) if you had a syntax 
that is vaguely similar to conventional syntax. ie you have a set of things like this:

<? if ?>
    ...
<? else ...?>

<? endif ?>

The problem with the widget syntax is that all the arguments
get squashed into the head of the definition as keyword arguments
and not placed syntactically in the body of the statement.

If you had a syntax that reminded people of PHP you'd suddenly get
thousands of new enthusiasts.

Unfamiliar syntax scares off beginners - been there - done that

Cheers

/Joe


 
 
TiddlyWiki does allow a lot of flexibility using recursion and, in my opinion at least, is something like declarative or functional programming.


That said, you can do if-then-else using lists like this:

<$list filter='condition statement' emptyMessage=<<Else result>>>
Than result
</$list>

as long as you can define your condition as a filter that returns one or more results if true and no results if false. If you are comfortable with set theory than the filtering operations in tiddlywiki are very powerful.

I'm familiar with list comprehensions - in fact my first WOW moment was when I realised
that <<list-links "[tag[xyz]]">> was just predicate logic with a funny syntax.

The filter is "just" a limited sub-set of Prolog with a kind of list-comprehension syntax.

The feeling I get playing with tiddly wiki is that it is some kind of beautiful mixture of
smalltalk and prolog and the browser DOM - all mixed together in very clever ways.



 

Joe Armstrong

ongelezen,
19 mrt 2018, 06:49:5219-03-2018
aan TiddlyWiki


On Monday, 19 March 2018 10:41:20 UTC+1, PMario wrote:
On Sunday, March 18, 2018 at 11:01:58 PM UTC+1, Joe Armstrong wrote:

4) Execute a loop

In TW everything, that needs looping is done with the <$list widget.

It's main purpose is to iterate over a defined set of tiddlers.
The set of tiddlers is defined using the filter-attribute. eg:

<$list filter="one two three">
<
<currentTiddler>>,
</$list>


The "loop-variable" can be defined:

<$list filter="one two three" variable="loop-var">
<
<loop-var>>,
</$list>



You mean

    <? for i in ["one", "two", "three"] ?>
         ${i}
     <? end for ?>

 
The list widget can be nested:

<$list filter="one two three" variable="outer">
  <$list filter="a b c" variable="inner">

   <
<outer>>-<<inner>>
 
</$list>
</$list>


Lists can be super complex. ... So these examples, only scratch the surface of the possibilities.

You mean

<? for i = ["one", "two","three"] ?>
   <? for j = ["a","b","c"] ?>
       ${i}-${j}
   <? end for ?>
<? end for ?>

Or

[ I ++ "-" ++ J || I<- ["one","two""three}, J <- ["a","b","c"]]

In a language which I am very familiar :-)

In my world this is not called "looping" it's called
a list-comprehension and is one of the corner stones of functional programming
and is equivalent to a findall construct in Prolog (almost)

In yet another syntax this is

findall
   ${I} ++ "-"  ++ ${J}
such that
    I <- ["one", "two", "three"]
    J <- ["a","b","cv"]

or something

I think a book (or a whatever) about TW has to explicitly build a bridge
between the TW syntax and the syntaxes with which the reader is familiar.
 


have fun!


I am - this feels like learning smalltalk (scratch the surface and there is
a lot hidden underneath)

Cheers

/Joe

 
mario

Xavier Cazin

ongelezen,
19 mrt 2018, 07:02:2419-03-2018
aan tiddl...@googlegroups.com
Hi Joe,

At the surface, the widget-oriented language of TiddlyWiki is just another domain specific language (https://en.wikipedia.org/wiki/Domain-specific_language) that we have to learn by doing, little by little. SQL or LaTeX are not of the generic kind either, but they are quite adapted to their tasks.

Now the fact that Javascript lays underneath allows you to add any feature that you consider missing. For instance, Evan Balster made a "condition" plugin as part as his work on Excel formulas (yet another DSL!) which is less domain specific than the RevealWidget approach: http://evanbalster.com/tiddlywiki/formulas.html#%24%3A%2Fplugins%2Febalster%2Fcondition

Cheers,
Xavier.

-- Xavier Cazin

--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+unsubscribe@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/4030277d-2605-4b12-9348-3199c7c6d9d0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jed Carty

ongelezen,
19 mrt 2018, 07:05:4719-03-2018
aan TiddlyWiki
TiddlyWiki isn't supposed to be a programming language. You may be able to extend it so that it can be used as one, but it is a mark-up language that has some advanced templating and macro features. That is very similar to a programming language and I have absolutely on interest in getting into a philosophical discussion about where one ends and the other starts. In practical terms it means that the things that tiddlywiki can do come from what it is used for and are designed to be useful and efficient in that context. Trying to impose structures from other programming languages on top of it is just extra clutter.

Going back to the if then else structure, there are multiple ways to emulate that in wikitext, adding in a completely new syntax structure to support how it is done in programming languages is unnecessary and has very limited use because it doesn't actually support what tiddlywiki is. I think that TiddlyWiki uses some new applications of concepts (as much as anything can be 'new' when it is based on thousands of years of history and learning) and that trying to force it to be more like other things is a bad direction to go in.

I am not against doing interesting things with tiddlywiki, I made an interactive fiction engine using it and I use it to drive my robot, but saying that it needs to be like something else we understand isn't doing something new, it is making tiddlywiki into something old.

PMario

ongelezen,
19 mrt 2018, 08:44:1419-03-2018
aan TiddlyWiki
On Monday, March 19, 2018 at 11:36:30 AM UTC+1, Joe Armstrong wrote:

Now there are 10 quadzilion template languages that can do this.
For example in django I might write

{% if has_tag("foo") %}
   <h1>hello I'm a foo</h1>
   ...
{% else %}
   <h1>hello<>h1>
{% endif %}


This can be done by using the list-widget

<ul>
<$list filter="[tag[foo]]" variable="you-name-it" emptyMessage="no tiddler tagged foo">
   
<li>hello I'm a foo - <<you-name-it>></li>
</$list><$list filter="[!tag[foo]]">
   
<li>hello I'm NOT tagged foo - <<currentTiddler>></li>
</$list>
<ul>

Just one possibility. ... There may be others.

-mario




PMario

ongelezen,
19 mrt 2018, 08:47:3819-03-2018
aan TiddlyWiki
On Monday, March 19, 2018 at 11:36:30 AM UTC+1, Joe Armstrong wrote:

The TW equivalent has no variable i but seems to have an implicit variable

It's just a convention, that every widget uses sensible defaults. ... This concept has proven to be sensible for non-programmers.
 
<<currentTiddler>> which is somewhat confusing.

currentTiddler is just there for convenience, so users don't need to type much.

-m


@TiddlyTweeter

ongelezen,
19 mrt 2018, 09:09:1719-03-2018
aan TiddlyWiki
I just wanted to add -- as a non-programmer -- that conceptions of TiddlyWiki "programming language" is grasped best by understanding FUNCTIONAL AIMS.

In one way you could say its "programming" is a mess. But IS it programming?

1 - Its unusual, in my limited understanding, for being able to change the way it changes itself (Quine).

2 - Its basic "logic" I think owes much to Regular Expressions. Looked at one way its a "Recursive Intelligent, self-re-Building, Regular Expression Engine". I think some of the same issues that can boggle programmers about regular expressions (their often indeterminable determinacy) also apply.

3 - Concerning scalability I have one word: "Transclusion."

So, IMO to grasp what it does its easiest to work backwards from final render to how it performed that. and look at the levels within that process.

My two cents.
Josiah
Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten