Any system of subset module imports in haskell? - perl

With Perl's standard module exports you can specify sets of functions to export/import by tag.
So, you can have sets like :all or :private or :test or whatever.
Hisorically, the advice seems to be that if you just want some semi-private functions then have an "inner" module and re-export the "default" parts of it.
I'm starting to want a semi-private interface for testing purposes. So, is that still the way to do it, or am I missing something in a recent ghc?

No, it's not possible in Haskell.
The Haskell solution to this problem is just to have separate modules. E.g.
module Foo.Private
( private1
, private2
, private3
) where
...
module Foo
( public1
, public2
, public3
) where
...
Additionally, if you want Foo.Private to re-export everything from Foo:
module Foo.Private
( module Foo
, private1
, private2
, private3
) where
import Foo
...
But if Foo in turn imports Foo.Private, then you'll have either to use
recursive modules, or to move the actual definitions to a third module,
say Foo.Base, which would be imported by Foo and Foo.Private.
Also, unlike perl, ghc doesn't support defining multiple modules in the same
file yet, which makes this solution somewhat heavyweight.

You can import a subset of functions using this:
import Data.List (nub, sort)
That will only import nub and sort from the Data.List module. Say, in case you don't want something to be imported, then:
import Data.List hiding (sort)
This will import all the functions in Data.List module except the sort function.
And in case if you want to export only certain functions from your module then define them like this:
module Data.List
( sort,
nub
) where
That will only export the sort and the nub functions in the module.

the haskell version of doctest runs code such that the non-exported functions are in scope.

Related

Elixir: Difference between require and import

What is the difference between require and import?
iex> require Integer
Integer
iex> Integer.is_odd(3)
true
and
iex> import List, only: [duplicate: 2]
List
iex> duplicate :ok, 3
[:ok, :ok, :ok]
It seems that they both do the same thing... get macros or functions from other modules.
From the documentation:
We use import whenever we want to easily access functions or macros from other modules without using the fully-qualified name.
also
Note that importing a module automatically requires it.
So if you import Integer, you can directly call is_odd, you don't need Integer.is_odd
require
According to this article:
Macro function is being evaluated during compilation. If you want to use it, you need to compile it first. This is exactly what require does.
In the background, it also gives an alias to required module, which means that you can pass as option just like with alias:
require TestModule, as: Test
import
The import directive allows you to skip the module part by importing all or some of the functions/macros:
import IO, only: [puts: 1]
puts "Hello"
As already mentioned, it also calls require in the background to compile it first.

how to import a module in Yang

I'm trying to build a CLI. I choose to use 'yang' to do so. I'm new to it and can't find out how to import existing moduls. As exemple I found a module for ospf on github (https://github.com/YangModels/yang/blob/master/vendor/cisco/xe/1631/ietf-ospf.yang) and I would like to import it in my own moduls. Can this be done? how?
EDIT1:
module mininet {
/* name space */
namespace "http://tail-f.com/ns/example/mininet";
prefix mininet;
import ietf-ospf {
prefix ospf;
revision-date 2015-03-09
}
leaf area-id-type {
type yang:area-id-type;
}
}
So I tried to do it this way using Piotr Babij help. Unfortunately this isn't working. What do I need to change?
area-id-type is a typedef of ietf-ospf. The error I have is te following one:
mininet.yang:12:3: error: trailing garbage after module
mininet.yang:12:3: error: unterminated statement
You may import other modules in your own modules by using the import statement. It is described in both RFC 7950 for YANG 1.1 and in RFC 6020 for YANG 1.0. In YANG 1.1 you may import two different revisions of the same module. Other that that, the import statement works the same in both versions.
In practice the basic import looks like this:
module acme-system {
namespace "http://acme.example.com/system";
prefix "acme";
import ietf-yang-types {
prefix "yang";
revision-date 2013-07-15;
}
leaf acme-ip-address {
type yang:dotted-quad;
}
}
If you omit the optional revision-date statement then an undefined module revision is imported. So, in general, it is a good practive to use it.
The mandatory prefix statement lets you to refer to the things in the imported module. In the example the prefix of the imported ietf-yang-types module is yang and, thanks to that, it is clear that yang:dotted-quad refers to a type from that module. In your case you have set the prefix to ospf, so you should have ospf:area-id-type to refer to a type definition from that module. If you import multiple modules you need to ensure their prefixes are unique.
Additionally, you are importing the oldest available revision of the ietf-ospf module. I just hope that this is what you really want to do.
Anyway, once you import a module you are allowed to:
use any grouping and typedef defined at the top level in the imported module or its submodules.
use any extension, feature, and identity defined in the imported module or its submodules.
use any node in the imported module's schema tree in must, path, and when statements, or as the target node in augment and deviation statements.
In the above example the typedef dotted-quad from the ietf-yang-types is used in the acme-system module.

Perl6: implicit and explicit import

Is it possible to write a module in a way that when the module is used with no explicit import all subroutines are imported and when it is used with explicit import only theses explicit imported subroutines are available?
#!/usr/bin/env perl6
use v6;
use Bar::Foo;
# all subroutines are imported
sub-one();
sub-two();
sub-three();
#!/usr/bin/env perl6
use v6;
use Bar::Foo :sub-one, :sub-two;
sub-one();
sub-two();
# sub-three not imported
Give your subs both the special label :DEFAULT as well as a dedicated one when exporting, eg
unit module Bar;
sub one is export(:DEFAULT, :one) { say "one" }
sub two is export(:DEFAULT, :two) { say "two" }
Now, you can import all of them with a plain use Bar, or can select specific ones via use Bar :one;

TypeScript import * without creating aliases

In TypeScript, how do you "import *" from a file without creating any aliases?
E.g. I have a file "utils" with top-level exported functions and want to import all them without recreating the aliases for each function.
Something like this:
import * from "utils";
Is that possible?
You can't generate an automatic name, you have to give it a name that is local to your file. This is by design to give each file its own naming context.
// import the default export (something exported with "export default")
import Stuff from "./Stuff";
// import specific things only; aliases in this style are optional
import { ItemA as AliasA, ItemB as AliasB } from "./Stuff";
import { ItemA, ItemB } from "./Stuff";
// import everything at once, grouped under a common name
import * as Stuff from "./Stuff";
I ... want to import all them without recreating the aliases for each function.
It sounds like you'd want the third option from above.
but with this syntax creates an alias
I think the idea is that if you could just import everything while taking the names as they are defined, then you'd have naming collisions. The way this works, you're forced to choose a name for every import, leaving it up to the names you choose to avoid collisions rather than having the imports clobber each other.
I think you can do something similar, but only with .d.ts files. jquery.d.ts does it, but I'm not 100% solid on how it works. You can simply say:
// where the file is really Stuff.d.ts
import "./Stuff";
I think the idea is to create a "Utils" module, attach all the functions and/or classes to it, put export in front of them, then export that instead, such as
module Utils {
export function add(first:number, second:number):number {
return first+second
}
}
export = Utils
Though i haven't played around with the es6 module syntax in typescript yet, as it seems you're implying to use.
You're close it's:
import * as utils from 'utils';
No curly braces.

Scala JSR 223 importing types/classes

The following example fails because the definition for Stuff can't be found:
package com.example
import javax.script.ScriptEngineManager
object Driver5 extends App {
case class Stuff(s: String, d: Double)
val e = new ScriptEngineManager().getEngineByName("scala")
println(e.eval("""import Driver5.Stuff; Stuff("Hello", 3.14)"""))
}
I'm unable to find any import statement that allows me to use my own classes inside of the eval statement. Am I doing something wrong? How does one import classes to be used during eval?
EDIT: Clarified example code to elicit more direct answers.
The Scripting engine does not know the context. It surely can't access all the local variables and imports in the script, since they are not available in the classfiles. (Well, variable names may be optionally available as a debug information, but it is virtually impossible to use them for this purpose.)
I am not sure if there is a special API for that. Imports are different across various languages, so bringing an API that should fit them all can be difficult.
You should be able to add the imports to the eval-ed String. I am not sure if there is a better way to do this.